This is a python script for a Telegram chatbot that uses the OpenAI API to generate responses to user input
- A Telegram bot API key, obtained by creating a bot on Telegram.
- An OpenAI API key, obtained by creating an account on OpenAI.
- Python 3 and the following libraries: requests, json, os, threading
- Clone the repository and navigate to the directory
- Fill in the API_KEY, MODEL, BOT_TOKEN, CHATBOT_HANDLE and BOT_PERSONALITY in the script with your own API keys and desired settings
- Run the script with the command python chatbot.py
- Add the bot to a Telegram group, make sure he has ADMIN priviledges, and mention the bot's username in a message to generate a response from the OpenAI API.
- The script starts by importing the necessary libraries and setting up the API clients.
- The openAI() function is defined, which takes a user input (prompt) and sends a request to the OpenAI API to generate a response. The function then returns the response.
- The telegram_bot_sendtext() function is defined, which takes a message, a chat ID, and a message ID, and sends the message as a response to the Telegram group using the Telegram Bot API.
- The Chatbot() function is defined which retrieves the latest requests from users in a Telegram group, generates a response using the OpenAI API, and sends the response back to the group. The function retrieves the last ID message from the text file for the ChatGPT update, check for new messages in Telegram group, check that user mentionned chatbot's username in message, calling OpenAI API using the bot's personality and sending back response to telegram group.
The script also uses a few global variables such as the API key for OpenAI, the model to use with the OpenAI API, the Telegram bot API key, and the bot's personality as defined by a set of adjectives.
This script is provided as a starting point, and can be further customized and expanded upon as needed. How to use Ask_ChatGPTbot:
- Type "@ask_yourchatgptbotname ___" to interact with the bot and ask a question
- Type "/img ___" to get an AI generated image related to your question or statement
- Type "/setTone(serious) @ask_yourchatgptbotname ___" in order to specify the tone for the bot's response
- Continuous printing of "file exists" is NORMAL. I will change the code so it doesn't write it anymore as it seems to be confusing.
- Ensure that
chatbot.txt
exists and has the correct path. If the file is empty, write1
on the first line. - Verify that your BOT has ADMIN access to your group.
- Ensure in the code that you have replaced
ask_chatgptbot
with your BOT NAME. - Check
https://api.telegram.org/bot{ACCESS TOKEN}/getUpdates
to make sure you can see all messages sent to your bot and that there are no 404 errors. If you encounter errors, check with Telegram or recreate a new bot.
- Error: "Could not convert string to float: ''" Ensure that
chatbot.txt
exists and has the correct path. If the file is empty, clear it and write1
on the first line. - Ensure in the code that you have replaced
ask_chatgptbot
with your BOT NAME. - Check
https://api.telegram.org/bot{ACCESS TOKEN}/getUpdates
to make sure you can see all messages sent to your bot and that there are no 404 errors. If you encounter errors, check with Telegram or recreate a new bot. - Make sure you have set the correct
ALLOWED_GROUP_ID=['-100xxxxxxxx', '-1001xxxxxxxx1']
. You can check the group ID indata['result']
or directly onhttps://api.telegram.org/bot{ACCESS TOKEN}/getUpdates
.
This project is licensed under the MIT License.