transport.js
3.58 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const MODULE_NAME = 'TRANSPORT';
const MAX_LENGTH = 4096;
const DEFAULT_EXPIRED_MS = 60;
const Telegraf = require('telegraf');
const moment = require('moment');
const truncateLine = require('custom-truncate/lib/line');
const messagingService = require('komodo-center-messaging-client-lib');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const addressbook = require('./addressbook');
const bot = new Telegraf(config.telegram.token);
async function sendMessageViaTelegram(chatId, msg) {
const [head, tail] = truncateLine(msg, MAX_LENGTH);
try {
await bot.telegram.sendMessage(chatId, head);
} catch (e) {
logger.warn(`D16D620A ${MODULE_NAME}: Exception on sending message to telegram API server`, {
chatId,
msg,
errCode: e.code,
errMessage: e.message,
});
return;
}
if (tail) {
sendMessageViaTelegram(chatId, tail);
}
}
function sendToMessagingService(partner, msg) {
if (!msg || (typeof msg === 'string' && !msg.trim())) {
return;
}
if (messagingService && messagingService.onIncomingMessage) {
messagingService.onIncomingMessage({
me: bot.context.botInfo.username,
partner,
msg: msg.trim(),
});
}
}
bot.start((ctx) => ctx.reply('Selamat datang. Silahkan ketik "HELP" untuk bantuan.'));
bot.command('help', (ctx) => {
sendToMessagingService(
ctx.from.username + (config.username_suffix || '@telegram.org'),
'help',
);
});
bot.catch((err) => {
logger.warn('Error catched', { errCode: err.code, errMessage: err.message });
});
bot.on('text', async (ctx) => {
const me = ctx.botInfo.username;
const from = ctx.from.username + (config.username_suffix || '@telegram.org');
const msg = ctx.message.text;
const delaySecs = Math.floor(new Date() / 1000 - ctx.message.date);
if (!from || !msg) {
logger.verbose('Ignoring messages with empty from / msg');
return;
}
const isExpired = delaySecs > (config.expired_secs || DEFAULT_EXPIRED_MS);
if (isExpired) {
logger.verbose('Ignoring expired messages', {
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, from, msg,
});
return;
}
logger.info(
'Incoming message from Telegram transport', {
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, from, msg,
},
);
addressbook.put(from, ctx.chat.id);
if (messagingService && messagingService.onIncomingMessage) {
messagingService.onIncomingMessage({ me, partner: from, msg: msg.trim() });
}
});
async function send(partner, msg) {
const me = bot.context.botInfo.username;
if (!partner || !msg) {
return;
}
const chatId = await addressbook.get(partner);
if (!chatId) {
logger.info('Not sending message because partner does not exist on address book', {
transport: 'telegram', me, partner, msg,
});
return;
}
logger.info('Sending message via Telegram transport', {
transport: 'telegram', me, partner, msg,
});
// bot.telegram.sendMessage(chatId, msg);
sendMessageViaTelegram(chatId, msg);
}
bot.launch().then(() => {
logger.info(`Connected to Telegram Bot API as "@${bot.context.botInfo.username}"`);
});
messagingService.setTransport(exports);
exports.send = send;