From 38174f33b777392fe4f0cbd3e2f361588ae2d410 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sun, 8 Sep 2024 13:02:51 +0530 Subject: [PATCH] feat: Add group command to Telegram integration This commit introduces a new `/ask` command for the Telegram integration, enabling users to interact with the chatbot in group chats. The command retrieves the message following `/ask`, sends it to the chatbot for processing, and then replies to the group with the chatbot's response. The previous restriction of private chat interaction is removed, allowing for a more flexible and collaborative usage of the chatbot within groups. --- app/ui/package.json | 2 +- package.json | 2 +- server/src/integration/telegram.ts | 118 +++++++++++++++++------------ 3 files changed, 71 insertions(+), 51 deletions(-) diff --git a/app/ui/package.json b/app/ui/package.json index 1422d768..812153b6 100644 --- a/app/ui/package.json +++ b/app/ui/package.json @@ -1,7 +1,7 @@ { "name": "app", "private": true, - "version": "1.11.0", + "version": "1.11.1", "type": "module", "scripts": { "dev": "vite", diff --git a/package.json b/package.json index 00db9f0d..a141a028 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dialoqbase", - "version": "1.11.0", + "version": "1.11.1", "description": "Create chatbots with ease", "scripts": { "ui:dev": "pnpm run --filter ui dev", diff --git a/server/src/integration/telegram.ts b/server/src/integration/telegram.ts index 7a2af602..ddbf5911 100644 --- a/server/src/integration/telegram.ts +++ b/server/src/integration/telegram.ts @@ -11,6 +11,10 @@ import * as fs from "fs/promises"; import { convertOggToWave } from "../utils/ffmpeg"; import { telegramFormat } from "../utils/telegram-format"; type DialoqBaseContext = FileFlavor; + +const groupCommand = process.env.DQ_TG_GROUP_COMMAND || "ask" +const groupCommandRegex = new RegExp(`^\\/${groupCommand}\\s(.*)`); + export default class TelegramBot { static get clients() { return this._clients.values(); @@ -38,18 +42,12 @@ export default class TelegramBot { ]); bot.command("start", async (ctx) => { - if (ctx.chat.type !== "private") { - return ctx.reply("Hi, I can only work in private chats."); - } await ctx.replyWithChatAction("typing"); const message = await welcomeMessage(identifier); return await ctx.reply(message); }); bot.command("ping", (ctx) => ctx.reply("pong")); bot.command("clear", async (ctx) => { - if (ctx.chat.type !== "private") { - return ctx.reply("I can only work in private chats."); - } await ctx.replyWithChatAction("typing"); if (!ctx?.from?.id) { return await ctx.reply("I can't find your user id"); @@ -60,55 +58,43 @@ export default class TelegramBot { ); return await ctx.reply(response); }); - bot.on("message:text", async (ctx) => { - // check it's a group chat - if (ctx.chat.type !== "private") { - return ctx.reply("I can only work in private chats."); - } - await ctx.replyWithChatAction("typing"); - // set messaging type - const user_id = ctx.from.id; - const message = await telegramBotHandler( - identifier, - ctx.message.text, - user_id - ); - - if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { - return await ctx.reply(message); - } - - return await ctx.reply(telegramFormat(message), - { - parse_mode: "HTML", - }); - }); - - bot.on("message:voice", async (ctx) => { + bot.hears(groupCommandRegex, async (ctx) => { try { - if (ctx.chat.type !== "private") { - return ctx.reply("I can only work in private chats."); - } - await ctx.replyWithChatAction("typing"); + const user_id = ctx.from.id; + const [, message] = ctx.match + if (!message) { + return await ctx.reply("Please provide a question after /ask"); + } + const response = await telegramBotHandler( + identifier, + message, + user_id + ); - const file = await ctx.getFile(); - const path = await file.download(); - - const audioWav = await convertOggToWave(path); - const audio = await fs.readFile(audioWav); - - const response = await convertTextToAudio(audio); + if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { + return await ctx.reply(response); + } + return await ctx.reply(telegramFormat(response), + { + parse_mode: "HTML", + }); + } catch (e) { + console.log(e) + return await ctx.reply("Something went wrong") + } + }); + bot.on("message:text", async (ctx) => { + if (ctx.chat.type === "private") { + await ctx.replyWithChatAction("typing"); const user_id = ctx.from.id; - const message = await telegramBotHandler( identifier, - response.text, + ctx.message.text, user_id ); - if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { return await ctx.reply(message); } @@ -117,9 +103,43 @@ export default class TelegramBot { { parse_mode: "HTML", }); - } catch (error) { - console.log(error); - return await ctx.reply("Opps! Something went wrong"); + } + }); + + bot.on("message:voice", async (ctx) => { + if (ctx.chat.type == "private") { + try { + await ctx.replyWithChatAction("typing"); + + const file = await ctx.getFile(); + const path = await file.download(); + + const audioWav = await convertOggToWave(path); + const audio = await fs.readFile(audioWav); + + const response = await convertTextToAudio(audio); + + const user_id = ctx.from.id; + + const message = await telegramBotHandler( + identifier, + response.text, + user_id + ); + + + if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { + return await ctx.reply(message); + } + + return await ctx.reply(telegramFormat(message), + { + parse_mode: "HTML", + }); + } catch (error) { + console.log(error); + return await ctx.reply("Opps! Something went wrong"); + } } }); @@ -150,4 +170,4 @@ export default class TelegramBot { return false; } } -} +} \ No newline at end of file