first commit

master
cheetah 2 years ago
commit fd752fed6f

2
.gitignore vendored

@ -0,0 +1,2 @@
node_modules
package-lock.json

@ -0,0 +1,24 @@
{
"bottoken": "<BOT_TOKEN>",
"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" ]
]
}
}
}
}

@ -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'))

@ -0,0 +1,8 @@
{
"dependencies": {
"axios": "^0.21.1",
"moment": "^2.29.1",
"socket.io-client": "^4.0.2",
"telegraf": "^4.3.0"
}
}
Loading…
Cancel
Save