Commit ec7c8cb5c92ad279cc6abf6b93eaee6d69f4fa16

Authored by Adhidarma Hadiwinoto
1 parent 51787cbf7d
Exists in master

start handler

Showing 1 changed file with 2 additions and 0 deletions Inline Diff

1 "use strict"; 1 "use strict";
2 2
3 const DEFAULT_EXPIRED_MS = 60; 3 const DEFAULT_EXPIRED_MS = 60;
4 4
5 const Telegraf = require('telegraf'); 5 const Telegraf = require('telegraf');
6 const moment = require('moment'); 6 const moment = require('moment');
7 const messagingService = require('komodo-center-messaging-client-lib'); 7 const messagingService = require('komodo-center-messaging-client-lib');
8 8
9 const config = require('komodo-sdk/config'); 9 const config = require('komodo-sdk/config');
10 const logger = require('komodo-sdk/logger') 10 const logger = require('komodo-sdk/logger')
11 11
12 const addressbook = require('./addressbook'); 12 const addressbook = require('./addressbook');
13 13
14 const bot = new Telegraf(config.telegram.token); 14 const bot = new Telegraf(config.telegram.token);
15 15
16 bot.start((ctx) => ctx.reply('Selamat datang. Silahkan ketik "HELP" untuk bantuan.'));
17
16 bot.catch((err) => { 18 bot.catch((err) => {
17 logger.warn('Error catched', {err: err}); 19 logger.warn('Error catched', {err: err});
18 }) 20 })
19 21
20 bot.on('text', async (ctx) => { 22 bot.on('text', async (ctx) => {
21 const me = ctx.botInfo.username; 23 const me = ctx.botInfo.username;
22 const from = ctx.from.username + ( config.username_suffix || '@telegram.org' ); 24 const from = ctx.from.username + ( config.username_suffix || '@telegram.org' );
23 const msg = ctx.message.text; 25 const msg = ctx.message.text;
24 const delaySecs = Math.floor(new Date() / 1000 - ctx.message.date); 26 const delaySecs = Math.floor(new Date() / 1000 - ctx.message.date);
25 27
26 if (!from || !msg) { 28 if (!from || !msg) {
27 logger.verbose('Ignoring messages with empty from / msg'); 29 logger.verbose('Ignoring messages with empty from / msg');
28 return; 30 return;
29 } 31 }
30 32
31 const isExpired = delaySecs > (config.expired_secs || DEFAULT_EXPIRED_MS); 33 const isExpired = delaySecs > (config.expired_secs || DEFAULT_EXPIRED_MS);
32 if (isExpired) { 34 if (isExpired) {
33 logger.verbose( 35 logger.verbose(
34 'Ignoring expired messages', 36 'Ignoring expired messages',
35 { transport: 'telegram', ts: moment(ctx.date).format('YYYY-MM-DD HH:mm:ss'), delay_secs: delaySecs, is_expired: isExpired, chat_id: ctx.chat.id, me: me, from: from, msg: msg } 37 { transport: 'telegram', ts: moment(ctx.date).format('YYYY-MM-DD HH:mm:ss'), delay_secs: delaySecs, is_expired: isExpired, chat_id: ctx.chat.id, me: me, from: from, msg: msg }
36 ); 38 );
37 return; 39 return;
38 40
39 } 41 }
40 42
41 logger.info( 43 logger.info(
42 'Incoming message from Telegram transport', 44 'Incoming message from Telegram transport',
43 { transport: 'telegram', ts: moment(ctx.date).format('YYYY-MM-DD HH:mm:ss'), delay_secs: delaySecs, is_expired: isExpired, chat_id: ctx.chat.id, me: me, from: from, msg: msg } 45 { transport: 'telegram', ts: moment(ctx.date).format('YYYY-MM-DD HH:mm:ss'), delay_secs: delaySecs, is_expired: isExpired, chat_id: ctx.chat.id, me: me, from: from, msg: msg }
44 ); 46 );
45 47
46 addressbook.put(from, ctx.chat.id); 48 addressbook.put(from, ctx.chat.id);
47 49
48 if (messagingService && messagingService.onIncomingMessage) { 50 if (messagingService && messagingService.onIncomingMessage) {
49 messagingService.onIncomingMessage({ 51 messagingService.onIncomingMessage({
50 me: me, 52 me: me,
51 partner: from, 53 partner: from,
52 msg: msg.trim() 54 msg: msg.trim()
53 }) 55 })
54 } 56 }
55 }) 57 })
56 58
57 async function send(partner, msg) { 59 async function send(partner, msg) {
58 const me = bot.context.botInfo.username; 60 const me = bot.context.botInfo.username;
59 61
60 const chatId = await addressbook.get(partner); 62 const chatId = await addressbook.get(partner);
61 if (!chatId) { 63 if (!chatId) {
62 logger.info('Not sending message because partner does not exist on address book', {transport: 'telegram', me: me, partner: partner, msg: msg }); 64 logger.info('Not sending message because partner does not exist on address book', {transport: 'telegram', me: me, partner: partner, msg: msg });
63 } 65 }
64 66
65 logger.info('Sending message via Telegram transport', { transport: 'telegram', me: me, partner: partner, msg: msg }); 67 logger.info('Sending message via Telegram transport', { transport: 'telegram', me: me, partner: partner, msg: msg });
66 bot.telegram.sendMessage(chatId, msg); 68 bot.telegram.sendMessage(chatId, msg);
67 } 69 }
68 70
69 bot.launch().then(() => { 71 bot.launch().then(() => {
70 logger.info(`Connected to Telegram Bot API as "@${ bot.context.botInfo.username }"`); 72 logger.info(`Connected to Telegram Bot API as "@${ bot.context.botInfo.username }"`);
71 }); 73 });
72 74
73 messagingService.setTransport(exports); 75 messagingService.setTransport(exports);
74 76
75 exports.send = send; 77 exports.send = send;