forked from fmiccolis/GAS-conversational-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-main.js
79 lines (71 loc) · 2.38 KB
/
3-main.js
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
const BOT = new TelegramBot(
ScriptProperties.getProperty("botToken"), // ask @BotFather on telegram for your botToken
ScriptProperties.getProperty("webAppId"), // after deployment as webApp, AppScript will give you the webAppId
ScriptProperties.getProperty("spreadsheetId"), // in order to work the TelegramBot object need a spreadsheet
ScriptProperties.getProperty("devList") // a comma separated string with the telegramIds of the developers
);
BOT.addHandler("/start", "Start your journey with the bot", "command", start);
BOT.addHandler("/help", "View all commands you can use", "command", help);
BOT.addHandler("/conv", "start a conversation", "conversation", {
entry: newConversation,
states: [
{type: "str", handler: addName},
{type: "date", handler: addStartDate},
{type: "date", handler: addEndDate}
],
final: endConversation,
fallback: {
command: "/cancel",
handler: cancel
}
})
function doPost(e) {
BOT.dispatcher(JSON.parse(e.postData.contents));
}
function initializeBot() {
BOT.firstLaunch();
}
function generateEntities(text = "") {
let count = (text.match(/\//g) || []).length;
if(count === 0) return null;
var entities = [];
var searchFrom = 0;
for(let i = 0; i < count; i++) {
var offset = text.indexOf("/", searchFrom);
var length = text.indexOf(" ", searchFrom);
entities.push({
"offset": offset,
"length": length > -1 ? length : text.length,
"type": "bot_command"
})
searchFrom += length + 1;
}
return entities;
}
function simulateTelegramMessage(text) {
var firstDev = BOT.devList[0];
var fromObj = {"id": firstDev, "first_name": "FirstName", "last_name": "LastName", "username": "Username", "language_code": "en", "is_bot": false}
var chatObj = {"id": firstDev, "first_name": "FirstName", "last_name": "LastName", "username": "Username", "type": "private"}
return {
"update_id": 143823073,
"message": {
"from": fromObj,
"chat": chatObj,
"date": Math.floor(new Date().getTime()/1000),
"text": text,
"entities": generateEntities(text)
}
}
}
function testFunction() {
var contents = simulateTelegramMessage("30-03-2023");
//Logger.log(JSON.stringify(contents, null, 2));
BOT.dispatcher(contents);
}
function clearMessageSheet() {
BOT.resetReceivedSheet();
BOT.resetSentSheet();
}
function clearConversationSheet() {
BOT.expireConversation();
}