A Telegram bot using OpenAi API and Cloudflare Workers to chat with users.
- Cloudflare Workers account
- OpenAi API key
- Telegram account
- Wrangler installed and logged in
Create a Telegram bot using @BotFather and get the bot token.
git clone
wrangler kv:namespace create "CHAT_LIST"
kv-namespaces = [
{ binding = "CHAT", id = "<your KV namespace id>" }
]
wrangler secret put TG_TOKEN
wrangler secret put OPENAI_TOKEN
wrangler publish
Now you can get your own workers url. Set the webhook of your bot to the workers url.
https://api.telegram.org/<TG_TOKEN>/setWebhook?url=https://<your-workers-url>/bot<TG_TOKEN>
If you want to use white list, add white_list
in your KV namespace. The value is a JSON array of user id like this:
[123456789, 987654321]