Skip to content

Vinzgh25/openai-telegram-bot

 
 

Repository files navigation

Simple OpenAI text model Telegram bot

A simple Telegram bot wrapping OpenAI API text models (like ChatGPT), build with python-telegram-bot!

Just start a conversation and all messages in the chat will be used as inputs for ChatGPT.

Conversation/context is not stored permanently and will be removed when the bot is restarted.

Requirements

This bot was built with Python 3.11, python-telegram-bot and openai-python. Full list of Python requirements is in the requirements.txt file, you can use it to install all of them.

Configuration

Configuration is done through a .env file. You can copy example file .env.example as .env and fill required parameters.

cp .env.example .env

Telegram bot

Only required parameter is a bot token.

You can also restrict who can access the bot via ALLOWED_USERNAMES. You can specify multiple usernames delimited by space. If you don't want to restrict the bot at all you can remove this parameter or leave it empty.

BOT_TOKEN='<your secret bot token>'
ALLOWED_USERNAMES='myusername friendusername'

OpenAI API

One required parameter is API key.

OPENAI_TOKEN='<your secret API key>'

Through .env you can also configure other parameters:

  • OPENAI_MODEL - which model to use (gpt-3.5-turbo is used by default)
  • OPENAI_SYSTEM_MESSAGE - system message
  • OPENAI_CONTEXT_LIMIT - how many messages will be kept in the context aside from prompt, all messages will be kept if empty
  • OPENAI_INITIAL_MESSAGE - additional message added after system message to all conversations, can be empty for no additional messages

Note that gpt-3.5-turbo doesn't pay strong attention to system message, so changing it might not provide significant changes to responses. You can use OPENAI_INITIAL_MESSAGE to tweak initial behaviour of the model.

OPENAI_MODEL='gpt-3.5-turbo'
OPENAI_SYSTEM_MESSAGE='You are a helpful assistant.'
OPENAI_CONTEXT_LIMIT=1000
OPENAI_INITIAL_MESSAGE='You are a helpful assistant acting like 18th century butler,'

Commands

  • /start - prints initial message returned from the model for just system message and optional initial message, doesn't impact conversation context
  • /reset - resets current conversation and removes all context, other than system message
  • /promptset - set new custom prompt, changes both system message and custom initial message (overwrites both OPENAI_SYSTEM_MESSAGE and OPENAI_INITIAL_MESSAGE)
  • /promptreset - restore prompt to default
  • /promptget - get custom prompt, won't respond with default one to avoid leaking configuration to users
  • /promptremove - force-remove any prompts from the conversation, including one defined in configuration files
  • /cancel - cancels ongoing operation, applies only to /promptset

Running the bot

You can run the bot from the source code directly, or in a Docker container.

From source code

  1. Create a Telegram bot via BotFather
  2. Create OpenAI API key
  3. Install all packages from requirements.txt
  4. Fill .env file
  5. Run main.py file with Python

Docker

  1. Create a Telegram bot via BotFather
  2. Create OpenAI API key
  3. Fill .env file
  4. Run docker compose up -d --build in terminal

Note that .env file is used only for loading environment variables into Docker container through compose. The file itself isn't added to the container.

About

Telegram bot for your ChatGPT needs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 96.6%
  • Dockerfile 3.4%