From fd752fed6fff3cb4ffd060d75d65e675f39637d5 Mon Sep 17 00:00:00 2001 From: cheetah Date: Thu, 17 Feb 2022 20:43:05 +0000 Subject: [PATCH] first commit --- .gitignore | 2 ++ config.json | 24 +++++++++++++++ index.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 8 +++++ 4 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 config.json create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f19d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..b23aa67 --- /dev/null +++ b/config.json @@ -0,0 +1,24 @@ +{ + "bottoken": "", + "pager": { + "url": "http://127.0.0.1:3000/api/message/advanced", + "paramsNormal": { + "type": "duplex", + "routing": { + "device": "birdyslim", + "connectors": [ + [ "pocsag", "133708C" ] + ] + } + }, + "paramsPriority": { + "type": "duplex", + "routing": { + "device": "birdyslim", + "connectors": [ + [ "pocsag", "133708A" ] + ] + } + } + } +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..c096296 --- /dev/null +++ b/index.js @@ -0,0 +1,85 @@ +const config = require('./config.json') + +const { Telegraf, Markup } = require('telegraf') +const io = require("socket.io-client") +const axios = require('axios') +const socket = io(new URL(config.pager.url).origin) + +const bot = new Telegraf(config.bottoken) +bot.start((ctx) => ctx.reply('Welcome, write me a message')) +let assoc = {} + +function filter(txt) { + if (txt.length > 240) txt = txt.substring(0, 240) + return txt.replace(/[^\x00-\x7F]/g, "") +} + +function editStatus(msgId, msg) { + if (!assoc[msgId]) return + console.log(assoc[msgId]) + const [tgChatId, tgMsgId] = [assoc[msgId].tgchat, assoc[msgId].tgmsg] + assoc[msgId].newText = assoc[msgId].newText + '\n' + msg + console.log(assoc[msgId]) + bot.telegram.editMessageText(tgChatId, tgMsgId, null, assoc[msgId].newText).then(console.log) +} +socket.on('msgmgr:event', (eventType, eventData) => { + console.log(eventType, eventData) + switch (eventType) { + case 'status': { + const [msgId, uuid, status] = eventData + editStatus(msgId, `${ uuid } is ${ status }`) + } break; + case 'read': + editStatus(eventData, 'read') + break; + case 'response': { + const [msgId, response] = eventData + editStatus(msgId, 'response ' + response) + } break; + } +}) +bot.on('message', (ctx) => { + if (!ctx.update.message.text) return ctx.reply("not a textmessage") +// if (!ctx.update.message.type) + console.log(ctx.update.message.from) + const preview = `Preview: ${ filter(ctx.update.message.from.first_name) }:${ filter(ctx.update.message.text) }` + console.log(preview) + ctx.reply(preview, Markup.inlineKeyboard([ + Markup.button.callback('💬(Receive ACK)', 'normal'), + Markup.button.callback('🚨(Receive,Read,Response ACKs)', 'priority') + ])) +}) +bot.action('normal', async (ctx) => { + const origText = ctx.update.callback_query.message.text.substring(8+1) + await ctx.answerCbQuery() + await ctx.editMessageReplyMarkup(undefined) + let msgId = (await axios.post(config.pager.url, Object.assign({ ...config.pager.paramsNormal }, { payload: origText }))).data + assoc[msgId] = { + tgmsg: ctx.update.callback_query.message.message_id, + text: origText, + newText: `Preview: ${ origText }\n\n--[${ msgId }]--\n`, + tgchat: ctx.update.callback_query.message.chat.id + } + await ctx.editMessageText(assoc[msgId].newText) + await ctx.replyWithSticker('CAACAgIAAxkBAANwYJRBBO79JToCqzsjJi1DltHnaTcAAmYNAALviaBI68kAAd6sFEZIHwQ') +}) +bot.action('priority', async (ctx) => { + const origText = ctx.update.callback_query.message.text.substring(8+1) + await ctx.answerCbQuery() + await ctx.editMessageReplyMarkup(undefined) + let msgId = (await axios.post(config.pager.url, Object.assign({ ...config.pager.paramsPriority }, { payload: origText }))).data + assoc[msgId] = { + tgmsg: ctx.update.callback_query.message.message_id, + text: origText, + newText: `Preview: ${ origText }\n\n--[${ msgId }]--\n`, + tgchat: ctx.update.callback_query.message.chat.id + } + await ctx.editMessageText(assoc[msgId].newText) + await ctx.replyWithSticker('CAACAgIAAxkBAANwYJRBBO79JToCqzsjJi1DltHnaTcAAmYNAALviaBI68kAAd6sFEZIHwQ') +}) + +bot.launch() + +// Enable graceful stop +process.once('SIGINT', () => bot.stop('SIGINT')) +process.once('SIGTERM', () => bot.stop('SIGTERM')) diff --git a/package.json b/package.json new file mode 100644 index 0000000..4cd8832 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "axios": "^0.21.1", + "moment": "^2.29.1", + "socket.io-client": "^4.0.2", + "telegraf": "^4.3.0" + } +}