Deploy your own Telegram chatbot that utilizes ChatGPT for conversational AI. The chatbot is built using Python and based on AWS Serverless architecture backed by AWS Lambda to process commands and Dynamo DB table to store registred users. All infrastructure is managed by Terraform.
To run this chatbot, you will need:
- OpenAI platform token from https://platform.openai.com/account/api-keys
- AWS Account
- Python 3.x
- Terraform
- Docker
-
Clone the repository:
git clone https://github.com/vadymbat/chat_gpt_telegram_bot
-
Create a Telegram bot via BotFather. Register bot commands:
authorize
to login,deauthorize
to logout andreset
to remove bot message history. Save bot token for future steps.
authorize - login to use the bot
deauthorize - logout
reset - remove bot message history
-
Navigate to the terraform directory:
cd infra
-
Initialize Terraform:
terraform init
-
Apply Terraform, replace placeholders with your values, where
<telegram_user_auth_token>
some random password to activate the bot from telegram using command/authorize <telegram_user_auth_token>
:terraform apply -var 'open_ai_token=<open_ai_token>' -var 'telegram_bot_token=<telegram_bot_token>' -var 'telegram_user_auth_token=<telegram_user_auth_token>'
-
Configure telegram webhook to your lambda url, which you get from terraform output
lambda_function_url
:
curl -X POST https://api.telegram.org/bot<telegram_bot_token>/setWebhook?url=<your_lambda_url>
- Test the chatbot by sending a message to your Telegram bot!
- Run
/start
- Run
/authorize <telegram_user_auth_token>
- Start some conversation
- Use
/reset
to remove message history when switching topic - You can share your bot with friends, by sharing a link and
<telegram_user_auth_token>
- Use
/deauthorize
to logout
- You need to send messages to keep container alive otherwise, the message history will be lost in about 3 minutes.
Congratulations, you've successfully deployed a Telegram chatbot that utilizes ChatGPT on AWS Lambda with Terraform! Feel free to customize the chatbot's