Commit aec55a47294f9c948d9b21c75794ba648713bcda
1 parent
621ac75692
Exists in
master
Address book put without suffix
Showing 2 changed files with 3 additions and 1 deletions Inline Diff
lib/addressbook.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const REDIS_TTL_SECS = 3600 * 24 * 7; | 3 | const REDIS_TTL_SECS = 3600 * 24 * 7; |
4 | 4 | ||
5 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
6 | 6 | ||
7 | const redis = require('redis'); | 7 | const redis = require('redis'); |
8 | const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' }); | 8 | const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' }); |
9 | 9 | ||
10 | const _caches = {}; | 10 | const _caches = {}; |
11 | 11 | ||
12 | function _composeKeyword(partner) { | 12 | function _composeKeyword(partner) { |
13 | return `TELEGRAM_ADDRESSBOOK_NAKAMICHIvsMiFA_${ config.telegram.token }_${ partner }`; | 13 | return `TELEGRAM_ADDRESSBOOK_NAKAMICHIvsMiFA_${ config.telegram.token }_${ partner }`; |
14 | } | 14 | } |
15 | 15 | ||
16 | function get(partner) { | 16 | function get(partner) { |
17 | partner = partner.toUpperCase(); | 17 | partner = partner.toUpperCase().replace(/@.*$/, ''); |
18 | return new Promise(function(resolve) { | 18 | return new Promise(function(resolve) { |
19 | if (_caches[partner]) { | 19 | if (_caches[partner]) { |
20 | resolve(_caches[partner]); | 20 | resolve(_caches[partner]); |
21 | } | 21 | } |
22 | else { | 22 | else { |
23 | const keyword = _composeKeyword(partner); | 23 | const keyword = _composeKeyword(partner); |
24 | 24 | ||
25 | redisClient.get(keyword, function(err, reply) { | 25 | redisClient.get(keyword, function(err, reply) { |
26 | if (err) { | 26 | if (err) { |
27 | resolve(null); | 27 | resolve(null); |
28 | return; | 28 | return; |
29 | } | 29 | } |
30 | 30 | ||
31 | if (reply) { | 31 | if (reply) { |
32 | resolve(Number(reply)); | 32 | resolve(Number(reply)); |
33 | _caches[partner] = Number(reply); | 33 | _caches[partner] = Number(reply); |
34 | } | 34 | } |
35 | else { | 35 | else { |
36 | resolve(null); | 36 | resolve(null); |
37 | } | 37 | } |
38 | }) | 38 | }) |
39 | } | 39 | } |
40 | }) | 40 | }) |
41 | 41 | ||
42 | } | 42 | } |
43 | 43 | ||
44 | function put(partner, chatId) { | 44 | function put(partner, chatId) { |
45 | partner = partner.toUpperCase().replace(/@.*$/, ''); | ||
45 | if (!partner || !chatId) { | 46 | if (!partner || !chatId) { |
46 | return; | 47 | return; |
47 | } | 48 | } |
48 | 49 | ||
49 | partner = partner.toUpperCase(); | 50 | partner = partner.toUpperCase(); |
50 | _caches[partner] = chatId; | 51 | _caches[partner] = chatId; |
51 | 52 | ||
52 | const keyword = _composeKeyword(partner); | 53 | const keyword = _composeKeyword(partner); |
53 | redisClient.set(keyword, chatId, 'EX', REDIS_TTL_SECS); | 54 | redisClient.set(keyword, chatId, 'EX', REDIS_TTL_SECS); |
54 | } | 55 | } |
55 | 56 | ||
56 | exports.get = get; | 57 | exports.get = get; |
57 | exports.put = put; | 58 | exports.put = put; |
58 | 59 | ||
59 | 60 |
lib/transport.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const MAX_LENGTH = 4096; | 3 | const MAX_LENGTH = 4096; |
4 | const DEFAULT_EXPIRED_MS = 60; | 4 | const DEFAULT_EXPIRED_MS = 60; |
5 | 5 | ||
6 | const Telegraf = require('telegraf'); | 6 | const Telegraf = require('telegraf'); |
7 | const moment = require('moment'); | 7 | const moment = require('moment'); |
8 | const truncateLine = require('custom-truncate/lib/line'); | 8 | const truncateLine = require('custom-truncate/lib/line'); |
9 | const messagingService = require('komodo-center-messaging-client-lib'); | 9 | const messagingService = require('komodo-center-messaging-client-lib'); |
10 | 10 | ||
11 | const config = require('komodo-sdk/config'); | 11 | const config = require('komodo-sdk/config'); |
12 | const logger = require('komodo-sdk/logger') | 12 | const logger = require('komodo-sdk/logger') |
13 | 13 | ||
14 | const addressbook = require('./addressbook'); | 14 | const addressbook = require('./addressbook'); |
15 | 15 | ||
16 | const bot = new Telegraf(config.telegram.token); | 16 | const bot = new Telegraf(config.telegram.token); |
17 | 17 | ||
18 | bot.start((ctx) => ctx.reply('Selamat datang. Silahkan ketik "HELP" untuk bantuan.')); | 18 | bot.start((ctx) => ctx.reply('Selamat datang. Silahkan ketik "HELP" untuk bantuan.')); |
19 | 19 | ||
20 | bot.command('help', (ctx) => { | 20 | bot.command('help', (ctx) => { |
21 | sendToMessagingService( | 21 | sendToMessagingService( |
22 | ctx.from.username + ( config.username_suffix || '@telegram.org' ), | 22 | ctx.from.username + ( config.username_suffix || '@telegram.org' ), |
23 | 'help' | 23 | 'help' |
24 | ) | 24 | ) |
25 | }) | 25 | }) |
26 | 26 | ||
27 | bot.catch((err) => { | 27 | bot.catch((err) => { |
28 | logger.warn('Error catched', {err: err}); | 28 | logger.warn('Error catched', {err: err}); |
29 | }) | 29 | }) |
30 | 30 | ||
31 | bot.on('text', async (ctx) => { | 31 | bot.on('text', async (ctx) => { |
32 | const me = ctx.botInfo.username; | 32 | const me = ctx.botInfo.username; |
33 | const from = ctx.from.username + ( config.username_suffix || '@telegram.org' ); | 33 | const from = ctx.from.username + ( config.username_suffix || '@telegram.org' ); |
34 | const msg = ctx.message.text; | 34 | const msg = ctx.message.text; |
35 | const delaySecs = Math.floor(new Date() / 1000 - ctx.message.date); | 35 | const delaySecs = Math.floor(new Date() / 1000 - ctx.message.date); |
36 | 36 | ||
37 | if (!from || !msg) { | 37 | if (!from || !msg) { |
38 | logger.verbose('Ignoring messages with empty from / msg'); | 38 | logger.verbose('Ignoring messages with empty from / msg'); |
39 | return; | 39 | return; |
40 | } | 40 | } |
41 | 41 | ||
42 | const isExpired = delaySecs > (config.expired_secs || DEFAULT_EXPIRED_MS); | 42 | const isExpired = delaySecs > (config.expired_secs || DEFAULT_EXPIRED_MS); |
43 | if (isExpired) { | 43 | if (isExpired) { |
44 | logger.verbose( | 44 | logger.verbose( |
45 | 'Ignoring expired messages', | 45 | 'Ignoring expired messages', |
46 | { 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 | { 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 } |
47 | ); | 47 | ); |
48 | return; | 48 | return; |
49 | 49 | ||
50 | } | 50 | } |
51 | 51 | ||
52 | logger.info( | 52 | logger.info( |
53 | 'Incoming message from Telegram transport', | 53 | 'Incoming message from Telegram transport', |
54 | { 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 } | 54 | { 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 } |
55 | ); | 55 | ); |
56 | 56 | ||
57 | addressbook.put(from, ctx.chat.id); | 57 | addressbook.put(from, ctx.chat.id); |
58 | 58 | ||
59 | if (messagingService && messagingService.onIncomingMessage) { | 59 | if (messagingService && messagingService.onIncomingMessage) { |
60 | messagingService.onIncomingMessage({ | 60 | messagingService.onIncomingMessage({ |
61 | me: me, | 61 | me: me, |
62 | partner: from, | 62 | partner: from, |
63 | msg: msg.trim() | 63 | msg: msg.trim() |
64 | }) | 64 | }) |
65 | } | 65 | } |
66 | }) | 66 | }) |
67 | 67 | ||
68 | async function sendMessageViaTelegram(chatId, msg) { | 68 | async function sendMessageViaTelegram(chatId, msg) { |
69 | const [head, tail] = truncateLine(msg, MAX_LENGTH); | 69 | const [head, tail] = truncateLine(msg, MAX_LENGTH); |
70 | await bot.telegram.sendMessage(chatId, head); | 70 | await bot.telegram.sendMessage(chatId, head); |
71 | 71 | ||
72 | if (tail) { | 72 | if (tail) { |
73 | sendMessageViaTelegram(chatId, tail); | 73 | sendMessageViaTelegram(chatId, tail); |
74 | } | 74 | } |
75 | } | 75 | } |
76 | 76 | ||
77 | async function send(partner, msg) { | 77 | async function send(partner, msg) { |
78 | const me = bot.context.botInfo.username; | 78 | const me = bot.context.botInfo.username; |
79 | 79 | ||
80 | if (!partner || !msg) { | 80 | if (!partner || !msg) { |
81 | return; | 81 | return; |
82 | } | 82 | } |
83 | 83 | ||
84 | const chatId = await addressbook.get(partner); | 84 | const chatId = await addressbook.get(partner); |
85 | if (!chatId) { | 85 | if (!chatId) { |
86 | logger.info('Not sending message because partner does not exist on address book', {transport: 'telegram', me: me, partner: partner, msg: msg }); | 86 | logger.info('Not sending message because partner does not exist on address book', {transport: 'telegram', me: me, partner: partner, msg: msg }); |
87 | return; | ||
87 | } | 88 | } |
88 | 89 | ||
89 | logger.info('Sending message via Telegram transport', { transport: 'telegram', me: me, partner: partner, msg: msg }); | 90 | logger.info('Sending message via Telegram transport', { transport: 'telegram', me: me, partner: partner, msg: msg }); |
90 | // bot.telegram.sendMessage(chatId, msg); | 91 | // bot.telegram.sendMessage(chatId, msg); |
91 | sendMessageViaTelegram(chatId, msg); | 92 | sendMessageViaTelegram(chatId, msg); |
92 | } | 93 | } |
93 | 94 | ||
94 | function sendToMessagingService(partner, msg) { | 95 | function sendToMessagingService(partner, msg) { |
95 | if (!msg || (typeof msg === 'string' && !msg.trim())) { | 96 | if (!msg || (typeof msg === 'string' && !msg.trim())) { |
96 | return; | 97 | return; |
97 | } | 98 | } |
98 | 99 | ||
99 | if (messagingService && messagingService.onIncomingMessage) { | 100 | if (messagingService && messagingService.onIncomingMessage) { |
100 | messagingService.onIncomingMessage({ | 101 | messagingService.onIncomingMessage({ |
101 | me: bot.context.botInfo.username, | 102 | me: bot.context.botInfo.username, |
102 | partner: partner, | 103 | partner: partner, |
103 | msg: msg.trim() | 104 | msg: msg.trim() |
104 | }) | 105 | }) |
105 | } | 106 | } |
106 | } | 107 | } |
107 | 108 | ||
108 | bot.launch().then(() => { | 109 | bot.launch().then(() => { |
109 | logger.info(`Connected to Telegram Bot API as "@${ bot.context.botInfo.username }"`); | 110 | logger.info(`Connected to Telegram Bot API as "@${ bot.context.botInfo.username }"`); |
110 | }); | 111 | }); |
111 | 112 | ||
112 | messagingService.setTransport(exports); | 113 | messagingService.setTransport(exports); |
113 | 114 | ||
114 | exports.send = send; | 115 | exports.send = send; |