Skip to content

Instantly share code, notes, and snippets.

@jibeetz
Last active March 8, 2023 14:03
Show Gist options
  • Save jibeetz/d2aa59bc7bfa191fd29757243576f0c5 to your computer and use it in GitHub Desktop.
Save jibeetz/d2aa59bc7bfa191fd29757243576f0c5 to your computer and use it in GitHub Desktop.
Basic TelegramGPT
npm install node-telegram-bot-api openai
node index.js
--------------------
index.js
const TelegramBot = require('node-telegram-bot-api')
const { Configuration, OpenAIApi } = require('openai')
const OPENAI_TOKEN = <OPENAI-TOKEN>
const BOT_TOKEN = <BOT-TOKEN>
const configuration = new Configuration({
apiKey: OPENAI_TOKEN,
})
const openai = new OpenAIApi(configuration)
const token = BOT_TOKEN
const bot = new TelegramBot(token, { polling: true })
bot.on('message', async (msg) => {
const completion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: msg.text }],
})
bot.sendMessage(msg.chat.id, completion.data.choices[0].message.content)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment