-
Notifications
You must be signed in to change notification settings - Fork 12
/
code.gs
55 lines (49 loc) · 1.45 KB
/
code.gs
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
function doPost(e) {
var update = JSON.parse(e.postData.contents);
// Make sure this is update is a type message
if (update.hasOwnProperty('message')) {
var msg = update.message;
var chatId = msg.chat.id;
// Make sure the update is a command.
if (
msg.hasOwnProperty('entities') &&
msg.entities[0].type == 'bot_command'
) {
// If the user sends the /quote command
if (msg.text == '/quote') {
var url =
'https://quotesondesign.com/wp-json/wp/v2/posts/?orderby=rand';
var data = UrlFetchApp.fetch(url);
var posts = JSON.parse(data);
var post = posts.shift();
// Delete the html tags and \n (newline)
var cleanContent = post.content.rendered
.replace(/<(?:.|\n)*?>/gm, '')
.replace(/\n/gm, '');
// Format the quote
var quote =
'"' +
cleanContent +
'"\n — <strong>' +
post.title.rendered +
'</strong>';
var payload = {
method: 'sendMessage',
chat_id: String(chatId),
text: quote,
parse_mode: 'HTML',
};
var data = {
method: 'post',
payload: payload,
};
// Replace with your token
var API_TOKEN = '297019760:BAFbL3yMus67Qv3Xu6gQ7VB93Jq4dkVaGP4';
UrlFetchApp.fetch(
'https://api.telegram.org/bot' + API_TOKEN + '/',
data
);
}
}
}
}