Repository navigation
Expand file tree
/
Copy pathrequestHandler.js
More file actions
67 lines (56 loc) · 2.05 KB
/
Copy pathrequestHandler.js
File metadata and controls
67 lines (56 loc) · 2.05 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
var moment = require("moment");
function handler(response, postData, copLogic, config) {
console.log("Telegram bot was called.");
console.log("postData : " + postData);
//var content = {fruit : decodeURIComponent(postData)};
//content = JSON.stringify(content);
var message_info = {};
if(IsJsonString(postData)){
message_info = JSON.parse(postData);
}
if ("update_id" in message_info) {
console.log("My bot just get a message!!");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("OK");
response.end();
var chat_id = 0;
var message = "";
var date = 0;
if ("channel_post" in message_info) {
//(channel) text
chat_id = message_info.channel_post.chat.id;
message = message_info.channel_post.text;
date = message_info.channel_post.date;
} else if ("edited_channel_post" in message_info) {
//(channel) edit_text
//For avoid confusion. The bot don't check edited message.
} else if ("message" in message_info) {
//(chat, group) text
chat_id = message_info.message.chat.id;
if ("text" in message_info.message) {
//When invite bot in to group. there are no "text" in json.
message = message_info.message.text;
}
date = message_info.message.date;
} else if ("edited_message" in message_info) {
//(chat, group) edit_text
//For avoid confusion. The bot don't check edited message.
}
console.log("The chat id is : " + chat_id);
console.log("The text message is :" + message);
if (moment().unix() - parseInt(date) < 7) {
copLogic(chat_id, message, config);
}
} else {
console.log("This is not a good POST data.");
}
}
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
exports.handler = handler;