Commit 93dce0589ec673487e753123018259017cc7a56e

Authored by Adhidarma Hadiwinoto
1 parent 7754406ece
Exists in master

HELP telegram command handler

Showing 1 changed file with 21 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.')); 16 bot.start((ctx) => ctx.reply('Selamat datang. Silahkan ketik "HELP" untuk bantuan.'));
17 17
18 bot.command('help', (ctx) => {
19 sendToMessagingService(
20 ctx.from.username + ( config.username_suffix || '@telegram.org' ),
21 'help'
22 )
23 })
24
18 bot.catch((err) => { 25 bot.catch((err) => {
19 logger.warn('Error catched', {err: err}); 26 logger.warn('Error catched', {err: err});
20 }) 27 })
21 28
22 bot.on('text', async (ctx) => { 29 bot.on('text', async (ctx) => {
23 const me = ctx.botInfo.username; 30 const me = ctx.botInfo.username;
24 const from = ctx.from.username + ( config.username_suffix || '@telegram.org' ); 31 const from = ctx.from.username + ( config.username_suffix || '@telegram.org' );
25 const msg = ctx.message.text; 32 const msg = ctx.message.text;
26 const delaySecs = Math.floor(new Date() / 1000 - ctx.message.date); 33 const delaySecs = Math.floor(new Date() / 1000 - ctx.message.date);
27 34
28 if (!from || !msg) { 35 if (!from || !msg) {
29 logger.verbose('Ignoring messages with empty from / msg'); 36 logger.verbose('Ignoring messages with empty from / msg');
30 return; 37 return;
31 } 38 }
32 39
33 const isExpired = delaySecs > (config.expired_secs || DEFAULT_EXPIRED_MS); 40 const isExpired = delaySecs > (config.expired_secs || DEFAULT_EXPIRED_MS);
34 if (isExpired) { 41 if (isExpired) {
35 logger.verbose( 42 logger.verbose(
36 'Ignoring expired messages', 43 'Ignoring expired messages',
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 } 44 { 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 }
38 ); 45 );
39 return; 46 return;
40 47
41 } 48 }
42 49
43 logger.info( 50 logger.info(
44 'Incoming message from Telegram transport', 51 'Incoming message from Telegram transport',
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 } 52 { 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 }
46 ); 53 );
47 54
48 addressbook.put(from, ctx.chat.id); 55 addressbook.put(from, ctx.chat.id);
49 56
50 if (messagingService && messagingService.onIncomingMessage) { 57 if (messagingService && messagingService.onIncomingMessage) {
51 messagingService.onIncomingMessage({ 58 messagingService.onIncomingMessage({
52 me: me, 59 me: me,
53 partner: from, 60 partner: from,
54 msg: msg.trim() 61 msg: msg.trim()
55 }) 62 })
56 } 63 }
57 }) 64 })
58 65
59 async function send(partner, msg) { 66 async function send(partner, msg) {
60 const me = bot.context.botInfo.username; 67 const me = bot.context.botInfo.username;
61 68
62 const chatId = await addressbook.get(partner); 69 const chatId = await addressbook.get(partner);
63 if (!chatId) { 70 if (!chatId) {
64 logger.info('Not sending message because partner does not exist on address book', {transport: 'telegram', me: me, partner: partner, msg: msg }); 71 logger.info('Not sending message because partner does not exist on address book', {transport: 'telegram', me: me, partner: partner, msg: msg });
65 } 72 }
66 73
67 logger.info('Sending message via Telegram transport', { transport: 'telegram', me: me, partner: partner, msg: msg }); 74 logger.info('Sending message via Telegram transport', { transport: 'telegram', me: me, partner: partner, msg: msg });
68 bot.telegram.sendMessage(chatId, msg); 75 bot.telegram.sendMessage(chatId, msg);
69 } 76 }
70 77
78 function sendToMessagingService(partner, msg) {
79 if (!msg || (typeof msg === 'string' && !msg.trim())) {
80 return;
81 }
82
83 if (messagingService && messagingService.onIncomingMessage) {
84 messagingService.onIncomingMessage({
85 me: bot.context.botInfo.username,
86 partner: partner,
87 msg: msg.trim()
88 })
89 }
90 }
91
71 bot.launch().then(() => { 92 bot.launch().then(() => {
72 logger.info(`Connected to Telegram Bot API as "@${ bot.context.botInfo.username }"`); 93 logger.info(`Connected to Telegram Bot API as "@${ bot.context.botInfo.username }"`);
73 }); 94 });
74 95
75 messagingService.setTransport(exports); 96 messagingService.setTransport(exports);
76 97
77 exports.send = send; 98 exports.send = send;