A minimal example of a Telegram Bot running on a Cloudflare Worker.
This branch is licensed under the MIT License. Check the other branches for different licenses.
- Get your new bot token from @BotFather: https://core.telegram.org/bots#6-botfather
- Sign up to Cloudflare Workers: https://workers.cloudflare.com/
- In the Cloudflare Dashboard go to "Workers", then click "Create application" and then "Create worker"
- Choose a name and click "Deploy" to create the worker
- Click on "Configure worker" -> "Settings" -> "Variables"
- Add a new variable with the name
ENV_BOT_TOKENand the value of your bot token from @BotFather - Add a new variable with the name
ENV_BOT_SECRETand set the value to a random secret. See https://core.telegram.org/bots/api#setwebhook - Click on "Quick Edit" to change the source code of your new worker
- Copy and paste the code from bot.js into the editor
- Optional: Change the
WEBHOOKvariable to a different path. See https://core.telegram.org/bots/api#setwebhook - Click on "Save and Deploy"
- In the middle panel append
/registerWebhookto 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 text message to your Telegram bot
The bot will send the original message back with Echo: prepended.
If you want to change it, look at the function onMessage(). It receives a Message object and sends a text back:
/**
* Handle incoming Message
* https://core.telegram.org/bots/api#message
*/
function onMessage (message) {
return sendPlainText(message.chat.id, 'Echo:\n' + message.text)
}The file bot2.js contains an improved bot, that demonstrates how to react to commands, send and receive inline buttons, and create MarkdownV2-formatted text.
The file bot3.js contains an improved version that replies inline queries with voice messages.
The voice messages should be stored in OPUS format and .ogg in the cloud you most like.
The audio files are listed in a JSON array with the following structure in a KV namespace called NAMESPACE and with following content under the key input_files.
Go to Workers & Pages -> KV and create a new namespace. Add a new key input_files and store the JSON structure from below with your own audio files.
Now in Overview -> your-worker -> Settings -> Variables -> KV Namespace Bindings bind the namespace to a variable called NAMESPACE.
[
[
"File Name",
"URL",
duration,
"<tg-spoiler> caption </tg-spoiler>"
],
[
"test",
"https://example.com/my_file.ogg",
5,
"<tg-spoiler>Description in a spoiler</tg-spoiler>"
],
]The bot4 is a bot that randomly reacts to messages received. It demostrates how to use big reactions when the 🎉 emoji gets chosen.
MIT License
Copyright (c) 2024 github.com/cvzi/telegram-bot-cloudflare Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.