forked from dvishal485/price-tracker-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessagingFramework.js
200 lines (197 loc) · 6.53 KB
/
MessagingFramework.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
function deleteMessage(message, messageID, chatID) {
if (message == null) {
var message_id = messageID
var chat_id = chatID
} else {
var message_id = message.result.message_id
var chat_id = message.result.chat.id
}
try {
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/deleteMessage?message_id=' + message_id + '&chat_id=' + chat_id)
} catch (e) { }
}
function forceReply(message, data, id, context) {
try {
if (data == null) {
var chatId = String(id)
} else {
var chatId = data.message.chat.id
}
var reply = {
'chat_id': chatId,
'text': message,
'parse_mode': 'HTML',
'reply_markup': JSON.stringify({
'force_reply': true,
'input_field_placeholder': context
}),
'disable_web_page_preview': true,
'allow_sending_without_reply': true
}
var method = 'sendMessage';
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(reply)
}
var xt = UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/' + method, options);
var data = JSON.parse(xt.getContentText())
return data;
} catch (e) {
if (Error(e).message.split('blocked').length > 1 || Error(e).message.split('deactivated').length > 1) {
removeUser(chatId)
} else {
sendReport(`Error while sending message : ${Error(e).message}`)
}
return null
}
}
function sendButtonMessage(message, data, id, keyboardButtons) {
try {
if (data == null) {
var chatId = String(id)
} else {
var chatId = data.message.chat.id
}
if (keyboardButtons.length > 2) {
var newKeyboardArray = []
for (var i = 0; i < keyboardButtons.length; i = i + 2) {
if (i + 1 != keyboardButtons.length) {
newKeyboardArray.push([keyboardButtons[i], keyboardButtons[i + 1]])
} else {
newKeyboardArray.push([keyboardButtons[i]])
}
}
var keyboard = { "inline_keyboard": newKeyboardArray };
} else {
var keyboard = { "inline_keyboard": [keyboardButtons] };
}
try {
var replyToMsg = data.message.message_id
var reply = {
'chat_id': chatId,
'text': message,
'parse_mode': 'HTML',
'reply_to_message_id': replyToMsg,
'reply_markup': JSON.stringify(keyboard),
'disable_web_page_preview': true,
'allow_sending_without_reply': true
}
} catch (e) {
var reply = {
'chat_id': chatId,
'text': message,
'parse_mode': 'HTML',
'reply_markup': JSON.stringify(keyboard),
'disable_web_page_preview': true,
'allow_sending_without_reply': true
}
}
var method = 'sendMessage';
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(reply)
}
var xt = UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/' + method, options);
var data = JSON.parse(xt.getContentText())
return data;
} catch (e) {
if (Error(e).message.split('blocked').length > 1 || Error(e).message.split('deactivated').length > 1) {
removeUser(chatId)
} else {
sendReport(`Error while sending message : ${Error(e).message}`)
}
return null
}
}
function sendPhoto(url, caption, data, id) {
if (data == null) {
var chatId = String(id)
} else {
var chatId = data.message.chat.id
}
try {
var replyToMsg = data.message.message_id
reply = {
'chat_id': chatId,
'photo': url,
'caption': caption,
'parse_mode': 'HTML',
'reply_to_message_id': replyToMsg,
'disable_web_page_preview': true,
'allow_sending_without_reply': true
}
} catch (e) {
var reply = {
'chat_id': chatId,
'photo': url,
'caption': caption,
'parse_mode': 'HTML',
'disable_web_page_preview': true,
'allow_sending_without_reply': true
}
}
var method = 'sendPhoto';
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(reply)
}
try {
var xt = UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/' + method, options);
var data = JSON.parse(xt.getContentText())
return data;
} catch (e) {
sendReport('Error while sending photo!')
sendReport(Error(e).message)
return null
}
}
function sendMessage(message, data, id) {
try {
if (data == null) {
var chatId = String(id)
} else {
var chatId = data.message.chat.id
}
try {
var replyToMsg = data.message.message_id
reply = {
'chat_id': chatId,
'text': message,
'parse_mode': 'HTML',
'reply_to_message_id': replyToMsg,
'disable_web_page_preview': true,
'allow_sending_without_reply': true
}
} catch (e) {
var reply = {
'chat_id': chatId,
'text': message,
'parse_mode': 'HTML',
'disable_web_page_preview': true,
'allow_sending_without_reply': true
}
}
var method = 'sendMessage';
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(reply)
}
var xt = UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/' + method, options);
var data = JSON.parse(xt.getContentText())
return data;
} catch (e) {
if (Error(e).message.split('blocked').length > 1 || Error(e).message.split('deactivated').length > 1) {
removeUser(chatId)
} else {
sendReport(`Error while sending message : ${Error(e).message}`)
}
return null
}
}
function sendReport(message) {
return sendMessage(message, null, ownerid)
}