-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (47 loc) · 1.7 KB
/
index.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
const { Telegraf } = require('telegraf')
const config = require('./config.json');
const pchomeParser = require('./pchomeParser.js');
const webPreviewer = (function() {
return {
bot: new Telegraf(config.BOT_TOKEN),
chatID: null,
receiveTextMsg: null,
getMessageType: function (message) {
const pcHomeTestReg = /https:\/\/24h(\.m)*\.pchome\.com\.tw\/prod\//;
if (pcHomeTestReg.test(message)) {
return 'pchomeUrl';
}
return;
},
sendMessageToTelegram: function (chatID, message) {
this.bot.telegram.sendMessage(chatID, message);
},
handleReceiveMessage: async function(event) {
const reqBody = JSON.parse(event.body);
this.chatID = reqBody.message.chat.id;
this.receiveTextMsg = reqBody.message.text || null;
if (this.receiveTextMsg === null) {
return;
}
const msgType = this.getMessageType(this.receiveTextMsg);
if (typeof msgType === 'undefined') {
return;
}
if (msgType === 'pchomeUrl') {
try {
this.sendMessageToTelegram(this.chatID, 'is a pchomeUrl');
const pchomeInfo = await pchomeParser.getInfo(this.receiveTextMsg);
console.log(pchomeInfo);
} catch (e) {
console.log(e);
}
}
return new Promise(async resolve => {
console.log('resoleve');
})
},
};
})();
exports.handler = async function(event) {
await webPreviewer.handleReceiveMessage(event);
};