Your own Telegram bot, powered by GPT-4. Configure and run within minutes!
- A basic Python environment
- API Key for OpenAI GPT-4
- Telegram Web client or app along with a token for your bot
- You need a basic Python 3 environment, on your laptop or in the cloud. I use a $5/month Linux server on Linode (referral link) for that.
- Clone the repository:
git clone https://github.com/u1i/gpt-4-telegram-bot
- Create a virtual Python env for your project:
cd gpt-4-telegram-bot; virtualenv $PWD
- Activate the environment:
. ./bin/activate
- Install the required Python libraries
pip3 install -r requirements.txt
- Your bot will speak with humans, this could be just yourself as a start. Find a name that you like, and use the @botfather in Telegram to create a new bot – along with a profile picture, a description and a token. The token will look like this "6232091381:AAFdcgkKLN92BnPLYikFi5ZYCSQyAz76zXY"
- Edit the file
context.json
and describe the scenario, add instructions of what language to use, what to avoid, and the start of the conversation – this is, in essence, the prompt engineering that is passed into GPT-4 to set the context, the tone, and give the bot that 'personality' that you want.
- create a copy of the file
config.ini-example
and call itconfig.ini
- add your API key for OpenAI
- add your bot token from Telegram
- Run this command:
python3 gpt-bot.py
- Chat up your bot in Telegram
- As a safety measure, users need to be whitelisted in order to chat with your bot. It should respond with a message like "Hi Joe – unfortunately, I have no idea who you are. Please tell your friendly human to add ID 446815934 to the list of allowed users. Once this is done please enter /start again."
- Edit the file
users.ini
and add this numeric ID to the listallowed_users
. Specifially, add a "," (comma), followed by the ID. No blank spaces. - In Telegram
/start
your conversation again. The bot should now recognize you!