-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathindex.js
78 lines (62 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require('dotenv').config()
const { Configuration, OpenAIApi } = require("openai");
const { getImage, getChat } = require("./Helper/functions");
const { Telegraf } = require("telegraf");
const configuration = new Configuration({
apiKey: process.env.API,
});
const openai = new OpenAIApi(configuration);
module.exports = openai;
const bot = new Telegraf(process.env.TG_API);
bot.start((ctx) => ctx.reply("Welcome , You can ask anything from me"));
bot.help((ctx) => {
ctx.reply(
"This bot can perform the following command \n /image -> to create image from text \n /ask -> ank anything from me "
);
});
// Image command
bot.command("image", async (ctx) => {
const text = ctx.message.text?.replace("/image", "")?.trim().toLowerCase();
if (text) {
const res = await getImage(text);
if (res) {
ctx.sendChatAction("upload_photo");
// ctx.sendPhoto(res);
// ctx.telegram.sendPhoto()
ctx.telegram.sendPhoto(ctx.message.chat.id, res, {
reply_to_message_id: ctx.message.message_id,
});
}
} else {
ctx.telegram.sendMessage(
ctx.message.chat.id,
"You have to give some description after /image",
{
reply_to_message_id: ctx.message.message_id,
}
);
}
});
// Chat command
bot.command("ask", async (ctx) => {
const text = ctx.message.text?.replace("/ask", "")?.trim().toLowerCase();
if (text) {
ctx.sendChatAction("typing");
const res = await getChat(text);
if (res) {
ctx.telegram.sendMessage(ctx.message.chat.id, res, {
reply_to_message_id: ctx.message.message_id,
});
}
} else {
ctx.telegram.sendMessage(
ctx.message.chat.id,
"Please ask anything after /ask",
{
reply_to_message_id: ctx.message.message_id,
}
);
// reply("Please ask anything after /ask");
}
});
bot.launch();