Sign inSign up

mezorg/chatgpt-telegram-bot

By mezorg

•Updated almost 2 years ago

Another chatgpt-telegram-bot

Image
2

5.7K

mezorg/chatgpt-telegram-bot repository overview

⁠ChatGPT Telegram Bot

python-version openai-version license Publish Docker image

img_4.png

A Telegram bot⁠ that integrates with OpenAI's official ChatGPT⁠, DALL·E⁠ and Whisper⁠ APIs to provide answers. Ready to use with minimal configuration required.

⁠Screenshots

demo

⁠Features

  • (NEW!) Support change model (gpt-3.5-turbo / gpt-4) on the fly in telegram
  • Support markdown in answers
  • Reset conversation with the /reset command
  • Typing indicator while generating a response
  • Access can be restricted by specifying a list of allowed users
  • Docker and Proxy support
  • (NEW!) Image generation using DALL·E via the /image command
  • (NEW!) Transcribe audio and video messages using Whisper (may require ffmpeg⁠)
  • (NEW!) Automatic conversation summary to avoid excessive token usage
  • (NEW!) Track token usage per user - by @AlexHTW⁠
  • (NEW!) Get personal token usage statistics and cost per day/month via the /stats command - by @AlexHTW⁠
  • (NEW!) User budgets and guest budgets - by @AlexHTW⁠
  • (NEW!) Stream support
  • (NEW!) GPT-4 support
    • If you have access to the GPT-4 API, simply change the OPENAI_MODEL parameter to gpt-4
  • (NEW!) Localized bot language
    • Available languages :gb: :de: :ru: :tr: :it: :finland: :es: :indonesia: :netherlands: :cn: :taiwan: :vietnam: :iran:
  • (NEW!) Improved inline queries support for group and private chats - by @bugfloyd⁠
    • To use this feature, enable inline queries for your bot in BotFather via the /setinline command⁠

⁠Additional features - help needed!

If you'd like to help, check out the issues⁠ section and contribute!
If you want to help with translations, check out the Translations Manual⁠

PRs are always welcome!

⁠Prerequisites

⁠Getting started

⁠Configuration

Customize the configuration by copying .env.example and renaming it to .env, then editing the required parameters as desired:

ParameterDescription
OPENAI_API_KEYYour OpenAI API key, you can get it from here⁠
TELEGRAM_BOT_TOKENYour Telegram bot's token, obtained using BotFather⁠ (see tutorial⁠)
ADMIN_USER_IDSTelegram user IDs of admins. These users have access to special admin commands, information and no budget restrictions. Admin IDs don't have to be added to ALLOWED_TELEGRAM_USER_IDS. Note: by default, no admin (-)
ALLOWED_TELEGRAM_USER_IDSA comma-separated list of Telegram user IDs that are allowed to interact with the bot (use getidsbot⁠ to find your user ID). Note: by default, everyone is allowed (*)
⁠Optional configuration

The following parameters are optional and can be set in the .env file:

⁠Budgets
ParameterDescriptionDefault value
BUDGET_PERIODDetermines the time frame all budgets are applied to. Available periods: daily (resets budget every day), monthly (resets budgets on the first of each month), all-time (never resets budget). See the Budget Manual⁠ for more informationmonthly
USER_BUDGETSA comma-separated list of $-amounts per user from list ALLOWED_TELEGRAM_USER_IDS to set custom usage limit of OpenAI API costs for each. For *- user lists the first USER_BUDGETS value is given to every user. Note: by default, no limits for any user (*). See the Budget Manual⁠ for more information*
GUEST_BUDGET$-amount as usage limit for all guest users. Guest users are users in group chats that are not in the ALLOWED_TELEGRAM_USER_IDS list. Value is ignored if no usage limits are set in user budgets (USER_BUDGETS=*). See the Budget Manual⁠ for more information100.0
TOKEN_PRICE$-price per 1000 tokens used to compute cost information in usage statistics. Source: https://openai.com/pricing⁠0.002
IMAGE_PRICESA comma-separated list with 3 elements of prices for the different image sizes: 256x256, 512x512 and 1024x1024. Source: https://openai.com/pricing⁠0.016,0.018,0.02
TRANSCRIPTION_PRICEUSD-price for one minute of audio transcription. Source: https://openai.com/pricing⁠0.006

Check out the Budget Manual⁠ for possible budget configurations.

⁠Additional optional configuration options
ParameterDescriptionDefault value
ENABLE_QUOTINGWhether to enable message quoting in private chatstrue
ENABLE_IMAGE_GENERATIONWhether to enable image generation via the /image commandtrue
ENABLE_TRANSCRIPTIONWhether to enable transcriptions of audio and video messagestrue
PROXYProxy to be used for OpenAI and Telegram bot (e.g. http://localhost:8080)-
OPENAI_MODELThe OpenAI model to use for generating responsesgpt-3.5-turbo
ASSISTANT_PROMPTA system message that sets the tone and controls the behavior of the assistantYou are a helpful assistant.
SHOW_USAGEWhether to show OpenAI token usage information after each responsefalse
STREAMWhether to stream responses. Note: incompatible, if enabled, with N_CHOICES higher than 1true
MAX_TOKENSUpper bound on how many tokens the ChatGPT API will return1200 for GPT-3, 2400 for GPT-4
MAX_HISTORY_SIZEMax number of messages to keep in memory, after which the conversation will be summarised to avoid excessive token usage15
MAX_CONVERSATION_AGE_MINUTESMaximum number of minutes a conversation should live since the last message, after which the conversation will be reset180
VOICE_REPLY_WITH_TRANSCRIPT_ONLYWhether to answer to voice messages with the transcript only or with a ChatGPT response of the transcriptfalse
VOICE_REPLY_PROMPTSA semicolon separated list of phrases (i.e. Hi bot;Hello chat). If the transcript starts with any of them, it will be treated as a prompt even if VOICE_REPLY_WITH_TRANSCRIPT_ONLY is set to true-
N_CHOICESNumber of answers to generate for each input message. Note: setting this to a number higher than 1 will not work properly if STREAM is enabled1
TEMPERATURENumber between 0 and 2. Higher values will make the output more random1.0
PRESENCE_PENALTYNumber between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far0.0
FREQUENCY_PENALTYNumber between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far0.0
IMAGE_SIZEThe DALL·E generated image size. Allowed values: 256x256, 512x512 or 1024x1024512x512
GROUP_TRIGGER_KEYWORDIf set, the bot in group chats will only respond to messages that start with this keyword-
IGNORE_GROUP_TRANSCRIPTIONSIf set to true, the bot will not process transcriptions in group chatstrue
BOT_LANGUAGELanguage of general bot messages. Currently available: en, de, ru, tr, it, fi, es, id, nl, zh-cn, zh-tw, vi, fa. Contribute with additional translations⁠en

Check out the official API reference⁠ for more details.

⁠Installing

Clone the repository and navigate to the project directory:

git clone https://github.com/n3d1117/chatgpt-telegram-bot.git
cd chatgpt-telegram-bot
⁠From Source
  1. Create a virtual environment:
python -m venv venv
  1. Activate the virtual environment:
# For Linux or macOS:
source venv/bin/activate

# For Windows:
venv\Scripts\activate
  1. Install the dependencies using requirements.txt file:
pip install -r requirements.txt
  1. Use the following command to start the bot:
python bot/main.py
⁠Using Docker Compose

Run the following command to build and run the Docker image:

docker compose up
⁠Ready-to-use Docker images

You can also use the Docker image from Docker Hub⁠:

docker pull mezorg/chatgpt-telegram-bot:2023-05-07--58-23
docker run -it --env-file .env mezorg/chatgpt-telegram-bot
⁠Docker manual build
docker build -t chatgpt-telegram-bot .
docker run -it --env-file .env chatgpt-telegram-bot

⁠Credits

⁠Disclaimer

This is a personal project and is not affiliated with OpenAI in any way.

⁠License

This project is released under the terms of the GPL 2.0 license. For more information, see the LICENSE⁠ file included in the repository.

⁠❤️ Donate

This project is Open Source. You may use it under license.

You can make a donation / subscription, or say thanks in Telegram: https://t.me/ZorgeR⁠

⁠Boosty (Donate / Subscription) : https://boosty.to/zorgg⁠
⁠BTC:

img_1.png bc1qyymy3ufvq3c3uq2q4927ll6x4rhvdw8gxlydwc

⁠ETH:

img_2.png 0x0213A705065B193D14f1A3cd075977e28Da8F9B3

⁠DOGE:

img_3.png D9RbkgazaGhkT4FHkJtHoh4hDxkzAZwQnK

⁠TON (TONCOIN):

img.png EQA9WB-PZIpA076uMsw11Xcrnvo_nZJ0Eg-KQUYr_zMek3gh

Tag summary

Content type

Image

Digest

sha256:b18e9e3a5…

Size

81.6 MB

Last updated

almost 2 years ago

docker pull mezorg/chatgpt-telegram-bot