You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
3.3 KiB
JavaScript
86 lines
3.3 KiB
JavaScript
2 years ago
|
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'))
|