const config = require('./config.json') const { Telegraf, Markup } = require('telegraf') const io = require("socket.io-client") const axios = require('axios') const fs = require('fs') const socket = io(new URL(config.pager.url).origin) const bot = new Telegraf(config.bottoken || '1234567890:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') 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 const sPass = Math.floor( (new Date().valueOf() - assoc[msgId].date.valueOf() ) / 1e3) msg = `+${ sPass }s> ${ msg }` 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( config.deliveryModes.map((deliveryModeConfig, index) => Markup.button.callback(deliveryModeConfig.name, 'deliveryMode-' + index) ) )) }) for (let deliveryModeIndex in config.deliveryModes) { bot.action('deliveryMode-' + deliveryModeIndex, async (ctx) => { const origText = ctx.update.callback_query.message.text.substring(8+1) await ctx.answerCbQuery() await ctx.editMessageReplyMarkup(undefined) const deliveryModeData = config.deliveryModes[ deliveryModeIndex ] let msgId = ( await axios.post( new URL(config.pager.url).origin + '/api/message/' + (!!deliveryModeData.preset ? 'preset' : 'advanced'), Object.assign( !!deliveryModeData.preset ? { preset: deliveryModeData.preset } // backward compatibility : { ...deliveryModeData.params } , { payload: origText }) ) ).data assoc[msgId] = { date: new Date(), 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) }) } bot.launch() // Enable graceful stop process.once('SIGINT', () => bot.stop('SIGINT')) process.once('SIGTERM', () => bot.stop('SIGTERM')) const express = require('express') const appConfig = express() appConfig.use(express.json()) appConfig.use(express.static('html')) appConfig.use(express.static(__dirname + '/node_modules/@mdi/font')) /** CONFIG Routes */ appConfig.get('/config', async (req, res) => { return res.json(JSON.parse(fs.readFileSync('config.json'))) }) appConfig.get('/api/deliveryPresets', async (req, res) => { const presets = await axios.get(new URL(config.pager.url).origin + '/api/deliveryPresets') return res.json(presets.data) }) appConfig.post('/config', async (req, res) => { if (!(!!req.body.bottoken)) return res.status(403).json(false) if (!(!!req.body.pager)) return res.status(403).json(false) console.log(req.body) fs.writeFileSync('config.json', JSON.stringify(req.body, null, "\t")) return res.json(true) }) appConfig.post('/restart', (req, res) => { process.exit(1) }) appConfig.listen(3060, '0.0.0.0' || config.host || '127.0.0.1')