-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWuNamesGenerator_bot.js
More file actions
98 lines (85 loc) · 2.94 KB
/
WuNamesGenerator_bot.js
File metadata and controls
98 lines (85 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const TELEGRAM_TOKEN = '*****************tg_api_token*****************';
const WEBAPP_URL = 'https://script.google.com/macros/s/*****************url_self-reference*****************/exec';
function setWebhook() {
const url = 'https://api.telegram.org/bot' + TELEGRAM_TOKEN + '/setWebhook?url=' + WEBAPP_URL;
const response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
}
function doPost(e) {
const update = JSON.parse(e.postData.contents);
const chatId = update.message.chat.id;
const text = update.message.text;
if (text === "/start") {
sendMessage(chatId, 'Now enter your full name');
} else if (!text) {
sendMessage(chatId, 'Responding only to text');
}
else {
const obj = getWuTangName(text);
let message;
if(obj.error) {
message = "Error:\r\n`" + obj.error_text + '`'
} else {
message = '`' + obj.oldname + "` from this day forward you will also be known as\r\n`" + obj.newname + '`';
}
sendMessage(chatId, message);
}
}
function sendMessage(chatId, text) {
const url = 'https://api.telegram.org/bot' + TELEGRAM_TOKEN + '/sendMessage';
const payload = {
'chat_id': chatId,
'text': text,
'parse_mode': 'Markdown'
};
const options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload)
};
UrlFetchApp.fetch(url, options);
}
function getWuTangName(name) {
try {
if (!name) {
return { error: true, error_text: "parameters not set" };
}
const payload = {
realname: name,
Submit: 'Enter+the+Wu-Tang'
};
const options = {
method: 'post',
payload: payload,
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch('http://www.mess.be/inickgenwuname.php', options);
const html = response.getContentText();
// Extract the relevant parts from HTML
const startMarker = '<font class=normalText>';
const endMarker = '</font>';
const startPos = html.indexOf(startMarker) + startMarker.length;
const endPos = html.indexOf(endMarker, startPos);
let content = html.substring(startPos, endPos).trim();
// Extract old name
const oldNameEnd = content.indexOf('</b>');
const oldName = content.substring(0, oldNameEnd).trim();
// Extract new name
const newNameStart = content.indexOf('<font size=2>') + '<font size=2>'.length;
const newNameEnd = content.indexOf('</b>', newNameStart);
let newName = content.substring(newNameStart, newNameEnd).trim();
// Clean up new name
newName = newName.replace(/\s+/g, ' '); // Replace multiple spaces with single space
newName = newName.replace(/([\w-]+).* ([\w-]+)/, '$1 $2'); // Simplify name format
return {
error: false,
oldname: oldName,
newname: newName
};
} catch (e) {
return {
error: true,
error_text: e.message
};
}
}