A minimal example of a Telegram Bot for ChatGPT running on a Cloudflare Worker.
- Get your new bot token from @BotFather: https://core.telegram.org/bots#6-botfather
- Get an OpenAI API key: https://platform.openai.com/account/api-keys
- Sign up to Cloudflare Workers: https://workers.cloudflare.com/
- In the Cloudflare Dashboard go to "Workers" and then click "Create a Service"
- Choose a name and click "Create a Service" to create the worker
- Click on "Quick Edit" to change the source code of your new worker
- Copy and paste the code from bot.js into the editor
- Replace the
TOKEN
variable in the code with your token from @BotFather - Replace the
API_KEY
variable in the code with your OpenAI API key - Optional: Change the
WEBHOOK
variable to a different path and theSECRET
variable to a random secret. See https://core.telegram.org/bots/api#setwebhook - Click on "Save and Deploy"
- In the middle panel append
/registerWebhook
to the url. For example: https://my-worker-123.username.workers.dev/registerWebhook - Click "Send". In the right panel should appear
Ok
. If 401 Unauthorized appears, you may have used a wrong bot token. - That's it, now you can send a ChatGPT prompt to your Telegram bot