Skip to content

🤖️ an AI chat Telegram bot can Web Search Powered by GPT-3.5/4/4 Turbo/4o, DALL·E 3, Groq, Gemini 1.5 Pro/Flash and the official Claude2.1/3/3.5 API using Python on Zeabur, fly.io and Replit.

License

Notifications You must be signed in to change notification settings

yym68686/ChatGPT-Telegram-Bot

Repository files navigation

🤖️ ChatGPT Telegram Bot

docker pull

The ChatGPT Telegram Bot is a powerful Telegram bot that utilizes the latest GPT models, including GPT-3.5/4/4 Turbo/4o, DALL·E 3, Groq Mixtral-8x7b/LLaMA2-70b, Gemini 1.5 Pro/Flash and the official Claude2.1/3/3.5 API. It enables users to engage in efficient conversations and information searches on Telegram.

✨ Features

  • Multiple AI Models: Supports GPT-3.5/4/4 Turbo/4o, DALL·E 3, Groq Mixtral-8x7b/LLaMA2-70b, Gemini 1.5 Pro/Flash and the official Claude2.1/3/3.5 API. Also supports one-api/new-api. Utilizes a self-developed API request backend SDK, independent of the OpenAI SDK.
  • Group Chat Topic Mode: Supports enabling topic mode in group chats, isolating APIs, dialogue history, plugin configurations, and preferences between topics.
  • Rich Plugin System: Supports web search(DuckDuckGo and Google), URL summarization, arXiv paper summarization, and code interpreter.
  • User-friendly Interface: Allows flexible model switching within the chat window and supports streaming output for a typewriter-like effect.
  • Efficient Message Processing: Asynchronously processes messages, answers questions in a multi-threaded manner, supports isolated dialogues, and provides unique dialogues for different users.
  • Multi-modal Q&A: Supports Q&A for images, PDF/TXT/MD documents.Users can upload files directly in the chat box for use.
  • Long Text Message Merging: Automatically merges long text messages, breaking through Telegram's single message length limit.
  • Multi-user Dialogue Isolation: Supports dialogue isolation and configuration isolation, allowing selection between multi-user and single-user modes.
  • Question Prediction: Automatically generates follow-up questions, anticipating what users might ask next.
  • Multi-language Interface: Supports Simplified Chinese, Traditional Chinese, and English interfaces.
  • Whitelist, Blacklist, and Admin Settings: Supports setting up whitelists, blacklists, and administrators.
  • Inline Mode: Allows users to @ the bot in any chat window to generate answers without needing to ask questions in the bot's chat window.
  • User-friendly Interface: Allows flexible model switching within the chat window and supports streaming output for a typewriter-like effect.Supports precise Markdown rendering of messages, utilizing another project of mine.
  • Convenient Deployment: Supports one-click koyeb, Zeabur, Replit deployment with true zero cost and idiot-proof deployment process. It also supports kuma anti-sleep, as well as Docker and fly.io deployment.

🍃 Environment variables

Variable Name Comment required?
BOT_TOKEN Telegram bot token. Create a bot on BotFather to get the BOT_TOKEN. Yes
API OpenAI or third-party API key. No
GPT_ENGINE Set the default QA model; the default is:gpt-4o. This item can be freely switched using the bot's "info" command, and it doesn't need to be set in principle. No
WEB_HOOK Whenever the telegram bot receives a user message, the message will be passed to WEB_HOOK, where the bot will listen to it and process the received messages in a timely manner. No
API_URL If you are using the OpenAI official API, you don't need to set this. If you using a third-party API, you need to fill in the third-party proxy website. The default is: https://api.openai.com/v1/chat/completions No
GROQ_API_KEY Groq offical API key. No
GOOGLE_AI_API_KEY Google AI offical API key. No
claude_api_key Claude offical API key. No
NICK The default is empty, and NICK is the name of the bot. The bot will only respond when the message starts with NICK that the user inputs, otherwise the bot will respond to any message. Especially in group chats, if there is no NICK, the bot will reply to all messages. No
PASS_HISTORY The default is False. The bot remembers the conversation history and considers the context when replying next time. If set to False, the bot will forget the conversation history and only consider the current conversation. Ensure the first letter of False and True is capitalized. No
GOOGLE_API_KEY If you need to use Google search, you need to set it. If you do not set this environment variable, the bot will default to provide duckduckgo search. Create credentials in Google Cloud's APIs & Services and the API Key will be GOOGLE_API_KEY on the credentials page. Google search can be queried 100 times a day, which is completely sufficient for light use. When the usage limit has been reached, the bot will automatically turn off Google search. No
GOOGLE_CSE_ID If you need to use Google search, you need to set it together with GOOGLE_API_KEY. Create a search engine in Programmable Search Engine, where the search engine ID is the value of GOOGLE_CSE_ID. No
whitelist Set which users can access the bot, and connect the user IDs authorized to use the bot with ','. The default value is None, which means that the bot is open to everyone. You can obtain your own Telegram ID and group ID through this bot. Please note that here we are referring to the Telegram ID, not the Telegram username, as they are different. The whitelist cannot contain group IDs, to authorize specific groups for use, the environment variable GROUP_LIST must be used. No
ADMIN_LIST Set up an admin list. Only admins can use the info command to configure the bot. When GROUP_LIST is set, only admins can have private chats with the bot. If ADMIN_LIST is not set, all users can modify the basic settings of the bot through the info command by default. Connect the admin IDs with a comma (','). You can obtain your own Telegram ID and group ID through this bot. No
GROUP_LIST Set up a list of groups that can use the bot. Connect the group IDs with a comma (','). After setting GROUP_LIST, except for the admin, no one else can have a private chat with the bot.You can obtain your own Telegram ID and group ID through this bot. No
CUSTOM_MODELS Set up a list of custom model name. Connect the model name with a comma (','). If you need to delete the default model, add a hyphen(-) before the default model name. No
CHAT_MODE Introduce multi-user mode, different users' configurations are not shared, the default is that each user has the same configuration, and the CHAT_MODE environment variable is used to control the configuration behavior. When CHAT_MODE is global, all users share the configuration. When CHAT_MODE is multiusers, user configurations are independent of each other. No

Koyeb Remote Deployment

One-click deployment:

Deploy to Koyeb

Zeabur Remote Deployment

One-click deployment:

Deploy on Zeabur

If you need follow-up function updates, the following deployment method is recommended:

  • Fork this repository first, then register for Zeabur. Currently, Zeabur does not support free Docker container deployment. If you need to use Zeabur to deploy the bot for this project, you will need to upgrade to the Developer Plan. Fortunately, Zeabur has introduced their sponsorship program, which offers a one-month Developer Plan to all contributors of this project. If you have features you'd like to enhance, feel free to submit pull requests to this project.
  • Import from your own Github repository.
  • Set the required environment variables, and redeploy.
  • If you need function updates in the follow-up, just synchronize this repository in your own repository and redeploy in Zeabur to get the latest functions.

Replit Remote Deployment

Run on Repl.it

After importing the Github repository, set the running command

pip install -r requirements.txt > /dev/null && python3 bot.py

Select Secrets in the Tools sidebar, add the environment variables required by the bot, where:

  • WEB_HOOK: Replit will automatically assign a domain name to you, fill in https://appname.username.repl.co
  • Remember to open "Always On"

Click the run button on the top of the screen to run the bot.

fly.io Remote Deployment

Official documentation: https://fly.io/docs/

Use Docker image to deploy fly.io application

flyctl launch --image yym68686/chatgpt:latest

Enter the name of the application when prompted, and select No for initializing Postgresql or Redis.

Follow the prompts to deploy. A secondary domain name will be provided in the official control panel, which can be used to access the service.

Set environment variables

flyctl secrets set BOT_TOKEN=bottoken
flyctl secrets set API=
# optional
flyctl secrets set WEB_HOOK=https://flyio-app-name.fly.dev/
flyctl secrets set NICK=javis

View all environment variables

flyctl secrets list

Remove environment variables

flyctl secrets unset MY_SECRET DATABASE_URL

ssh to fly.io container

flyctl ssh issue --agent
# ssh connection
flyctl ssh establish

Check whether the webhook URL is correct

https://api.telegram.org/bot<token>/getWebhookInfo

Docker Local Deployment

Start the container

docker run -p 80:8080 --name chatbot -dit \
    -e BOT_TOKEN=your_telegram_bot_token \
    -e API= \
    -e API_URL= \
    yym68686/chatgpt:latest

Or if you want to use Docker Compose, here is a docker-compose.yml example:

version: "3.5"
services:
  chatgptbot:
    container_name: chatgptbot
    image: yym68686/chatgpt:latest
    environment:
      - BOT_TOKEN=
      - API=
      - API_URL=
    ports:
      - 80:8080

Run Docker Compose container in the background

docker-compose pull
docker-compose up -d

# uni-api
docker-compose -f docker-compose-uni-api.yml up -d

Package the Docker image in the repository and upload it to Docker Hub

docker build --no-cache -t chatgpt:latest -f Dockerfile.build --platform linux/amd64 .
docker tag chatgpt:latest yym68686/chatgpt:latest
docker push yym68686/chatgpt:latest

One-Click Restart Docker Image

set -eu
docker pull yym68686/chatgpt:latest
docker rm -f chatbot
docker run -p 8080:8080 -dit --name chatbot \
-e BOT_TOKEN= \
-e API= \
-e API_URL= \
-e GOOGLE_API_KEY= \
-e GOOGLE_CSE_ID= \
-e claude_api_key= \
yym68686/chatgpt:latest
docker logs -f chatbot

This script is for restarting the Docker image with a single command. It first removes the existing Docker container named "chatbot" if it exists. Then, it runs a new Docker container with the name "chatbot", exposing port 8080 and setting various environment variables. The Docker image used is "yym68686/chatgpt:latest". Finally, it follows the logs of the "chatbot" container.

📄 Q & A

  • Why can't I use Google search?

By default, DuckDuckGo search is provided. The official API for Google search needs to be applied for by the user. It can provide real-time information that GPT could not answer before, such as today's trending topics on Weibo, today's weather in a specific location, and the progress of a certain person or news event.

  • How do I switch models?

You can switch between GPT3.5/4/4o, and other models using the "/info" command in the chat window.

  • Can it be deployed in a group?

Yes, it supports whitelist to prevent abuse and information leakage.

  • Why can't the bot talk when I add it to the group?

If this is the first time you add the bot to a group chat, you need to set the group privacy to disable in botfather, then remove the bot from the group chat and re-add it to use it normally.

The second method is to set the bot as an administrator, so the bot can be used normally. However, if you want to add the bot to a group chat where you are not an administrator, the first method is more suitable.

Another possibility is that the GROUP_LIST set is not the current group chat ID. Please check if GROUP_LIST is set; GROUP_LIST is the group ID, not the group name. The group ID starts with a minus sign followed by a string of numbers.

  • Why did I set GROUP_LIST, but others can't private chat?

Once the GROUP_LIST environment variable is set, by default, no one can private chat with the bot, only administrators can. If you need others to be able to private chat with the bot, you need to add them to the ADMIN_LIST. Please note that administrators have higher permissions to modify the bot's configuration. Once GROUP_LIST is set, regardless of whether the whitelist is set, no one can chat privately with the bot except administrators.

  • How should I set the API_URL?

The API_URL supports all suffixes, including: https://api.openai.com/v1/chat/completions, https://api.openai.com/v1, and https://api.openai.com/. The bot will automatically allocate different endpoints based on different uses.

  • Is it necessary to configure the web_hook environment variable?

The web_hook is not a mandatory environment variable. You only need to set the domain name (which must be consistent with WEB_HOOK) and other environment variables as required for your application's functionality.

  • I deployed a robot with docker compose. If the documentation is placed on the server locally, which directory should it be mounted to in order to take effect? Do I need to set additional configurations and modify the code?

You can directly send the documentation to the robot through the chat box, and the robot will automatically parse the documentation. To use the documentation dialogue function, you need to enable the historical conversation feature. There is no need for additional processing of the documentation.

  • I still can't get it to work... I want to use it in a group, I've set the ADMIN_LIST to myself, and the GROUP_LIST to that group, with the whitelist left empty. However, only I can use it in that group, other members in the group are prompted with no permission, what's going on?

Here's a troubleshooting guide: Please carefully check if the GROUP_LIST is correct. The ID of a Telegram group starts with a negative sign followed by a series of numbers. If it's not, please use this bot bot to reacquire the group ID.

  • I've uploaded a document, but it's not responding based on the content of the document. What's going on?

To use the document question and answer feature, you must first enable the history record. You can turn on the history record through the /info command, or by setting the environment variable PASS_HISTORY to True to enable the history record by default. Please note that enabling the history record will incur additional costs, so this project does not enable the history record by default. This means that the question and answer feature cannot be used under the default settings. Before using this feature, you need to manually enable the history record.

  • After setting the NICK, there's no response when I @ the bot, and it only replies when the message starts with the nick. How can I make it respond to both the nick and @botname?

In a group chat scenario, if the environment variable NICK is not set, the bot will receive all group messages and respond to all of them. Therefore, it is necessary to set NICK. After setting NICK, the bot will only respond to messages that start with NICK. So, if you want to @ the bot to get a response, you just need to set NICK to @botname. This way, when you @ the bot in the group, the bot will detect that the message starts with @botname, and it will respond to the message.

  • How many messages will the history keep?

All other models use the official context length settings, for example, the gpt-3.5-turbo-16k context is 16k, the gpt-4o context is 128k, and the Claude3/3.5 context is 200k. This limitation is implemented to save user costs, as most scenarios do not require a high context.

  • How to delete the default model name from the model list?

You can use the CUSTOM_MODELS environment variable to complete it. For example, if you want to add gpt-4o and remove the gpt-3.5 model from the model list, please set CUSTOM_MODELS to gpt-4o,-gpt-3.5.

  • How does conversation isolation specifically work?

Conversations are always isolated based on different windows, not different users. This means that within the same group chat window, the same topic, and the same private chat window, it is considered the same conversation. CHAT_MODE only affects whether configurations are isolated. In multi-user mode, each user's plugin configurations, preferences, etc., are independent and do not affect each other. In single-user mode, all users share the same plugin configurations and preferences. However, conversation history is always isolated. Conversation isolation is to protect user privacy, ensuring that users' conversation history, plugin configurations, preferences, etc., are not visible to other users.

  • Why hasn't the Docker image been updated for a long time?

The Docker image only stores the runtime environment of the program. Currently, the runtime environment of the program is stable, and the environment dependencies have hardly changed, so the Docker image has not been updated. Each time the Docker image is redeployed, it will pull the latest code, so there is no need to worry about the Docker image update issue.

  • Why does the container report an error "http connect error or telegram.error.TimedOut: Timed out" after starting?

This issue is likely caused by the server deploying Docker being unable to connect to the Telegram server or the instability of the Telegram server.

  1. In most cases, restarting the service, checking the server network environment, or waiting for the Telegram service to recover will suffice.
  2. Additionally, you might try communicating with the Telegram server via web hook, which might solve the problem.
  • How to make docker retry infinitely instead of stop at beginning?

The --restart unless-stopped parameter in Docker sets the container's restart policy. Specifically:

  1. unless-stopped: This policy means that the container will automatically restart if it stops, except when it is manually stopped. In other words, if the container stops due to an error or system reboot, it will automatically restart. However, if you manually stop the container (e.g., using the docker stop command), it will not restart on its own. This parameter is particularly useful for services that need to run continuously, as it ensures that the service will automatically recover from unexpected interruptions without requiring manual intervention.

  2. Example: Suppose you have a Docker container running a web server, and you want it to restart automatically if it crashes or if the system reboots, but not if you manually stop it. You can use the following command:

docker run -d --name my-web-server -p 80:80 --restart unless-stopped my-web-server-image

In this example, the web server container named my-web-server will restart automatically unless you manually stop it.

  • Switching models, do I need to re-enter the prompt?

Yes, because switching models will reset the history, so you need to re-enter the prompt.

  • Can Bot tokens have multiple tokens?

No, in the future it will support multiple Bot Tokens.

References

https://core.telegram.org/bots/api

https://github.com/acheong08/ChatGPT

https://github.com/franalgaba/chatgpt-telegram-bot-serverless

https://github.com/gpchelkin/scdlbot/blob/d64d14f6c6d357ba818e80b8a0a9291c2146d6fe/scdlbot/__main__.py#L8

The markdown rendering of the message used is another project of mine.

Sponsors

We are grateful for the support from the following sponsors:

  • @fasizhuanqian: 300 USDT

  • @ZETA: $250

  • @yuerbujin: ¥600

  • @IKUNONHK: 30 USDT

  • @Zeabur: $25

  • @Bill_ZKE: 20 USDT

How to Sponsor Us

If you would like to support our project, you can sponsor us through the following methods:

  1. PayPal

  2. USDT-TRC20, USDT-TRC20 Wallet Address: TLFbqSv5pDu5he43mVmK1dNx7yBMFeN7d8

  3. WeChat

  4. Alipay

Thank you for your support!

Star History

Star History Chart

License

This project is licensed under GPLv3, which means you are free to copy, distribute, and modify the software, as long as all modifications and derivative works are also released under the same license.

About

🤖️ an AI chat Telegram bot can Web Search Powered by GPT-3.5/4/4 Turbo/4o, DALL·E 3, Groq, Gemini 1.5 Pro/Flash and the official Claude2.1/3/3.5 API using Python on Zeabur, fly.io and Replit.

Topics

Resources

License

Stars

Watchers

Forks

Languages