-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
27 lines (23 loc) · 874 Bytes
/
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
require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const { Configuration, OpenAIApi } = require("openai");
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, { polling: true });
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
bot.onText(/\/start/, (msg) => {
bot.sendMessage(msg.chat.id, 'Hello! I am a bot that can answer your questions using the OpenAI API. How can I help you today?');
});
bot.on('message', async (msg) => {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: msg.text,
temperature: 0.7,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
bot.sendMessage(msg.chat.id, response.data.choices[0].text);
});