-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (71 loc) · 2.32 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
79
80
81
82
83
import dotenv from "dotenv";
import express from "express";
import bodyParser from "body-parser";
import {
generateResponse,
generateImage,
generateChatResponse,
} from "./utils/functions.js";
import { bot } from "./utils/bot.js";
dotenv.config();
const app = express();
app.use(express.json());
app.use(bodyParser.json());
app.use((err, req, res, next) => {
console.error(err.stack); // Log the error stack trace
res.status(500).send("An error occurred, the server will be reset.");
// Send an exit signal to the Node.js process
process.exit(1);
});
app.post("/img", async (req, res) => {
const prompt = req.body.prompt;
const response = await generateImage(prompt);
console.log(response);
res.send(response).status(200);
});
app.post("/bot", async (req, res) => {
const prompt = req.body.prompt;
const response = await generateResponse(prompt);
console.log(response);
res.send(response).status(200);
});
app.post("/prompt", async (req, res) => {
const prompt = req.body.prompt;
const response = await generateChatResponse(prompt);
console.log(response);
res.send(response).status(200);
});
app.listen(process.env.PORT || 3002, () => {
console.log("Server is running...");
});
bot.start((ctx) => {
ctx.reply(
"Welcome to the ChatGPT Bot! Send me a message, and I will generate a response using GPT-3."
);
});
bot.hears("hi", (ctx) => ctx.reply("Hey there"));
bot.hears(["Hi", "hi", "hey"], (ctx) => ctx.reply("Hey there"));
bot.command("fact", (ctx) => ctx.reply("👍"));
bot.command("voice", async (ctx) => {
ctx.replyWithVoice(
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"
);
});
bot.command("list", (ctx) =>
ctx.telegram.getMyCommands().then((res) => console.log(res))
);
bot.on("message", async (ctx) => {
if (ctx.message.text.split(" ")[0] === "/img") {
const imgPrompt = ctx.message.text.split(" ").slice(1).join(" ");
const imgResponse = await generateImage(imgPrompt);
return ctx.replyWithPhoto(imgResponse);
} else {
console.log(ctx.message.text);
const prompt = ctx.message.text;
const response = await generateChatResponse(prompt);
return ctx.reply(response);
}
});
bot.entity("hello", (ctx) => ctx.reply("Hey there"));
bot.launch((ctx) => ctx.telegram.setCommands(["fact", "voice"]));
console.log("Telegram ChatGPT Bot is running...");