Skip to content

Commit

Permalink
feat: Add group command to Telegram integration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
n4ze3m committed Sep 8, 2024
1 parent f597cfe commit 38174f3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion app/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "app",
"private": true,
"version": "1.11.0",
"version": "1.11.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
118 changes: 69 additions & 49 deletions server/src/integration/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import * as fs from "fs/promises";
import { convertOggToWave } from "../utils/ffmpeg";
import { telegramFormat } from "../utils/telegram-format";
type DialoqBaseContext = FileFlavor<Context>;

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();
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
Expand All @@ -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");
}
}
});

Expand Down Expand Up @@ -150,4 +170,4 @@ export default class TelegramBot {
return false;
}
}
}
}

0 comments on commit 38174f3

Please sign in to comment.