Commit 3ad7844ee8a7f003ff466443e3e1178e08666966

Authored by Adhidarma Hadiwinoto
1 parent 9f9deffd31
Exists in master

Penanganan kalau partner atau msg kosong

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