Giter Club home page Giter Club logo

cf-telegram-bot's Introduction

Another Cloudflare Workers Telegram Bot

Another Cloudflare Workers Telegram Bot is inspired by cvzi/telegram-bot-cloudflare. This is wrapped up as a class to make it easy to to used like any NPM library. You can handle telegram update the way you wish by override onUpdate method.

Examples are about the same demos from cvzi/telegram-bot-cloudflare.

Setup through Cloudflare GUI:

  1. Get your new bot token from @BotFather: https://core.telegram.org/bots#6-botfather
  2. Sign up to Cloudflare Workers: https://workers.cloudflare.com/
  3. In the Cloudflare Dashboard go to "Workers", then click "Create application" and then "Create worker"
  4. Choose a name and click "Deploy" to create the worker
  5. Click on "Configure worker" -> "Settings" -> "Variables"
  6. Add a new variable with the name ENV_BOT_TOKEN and the value of your bot token from @BotFather
  7. Add a new variable with the name ENV_BOT_SECRET and set the value to a random secret. See https://core.telegram.org/bots/api#setwebhook
  8. Click on "Quick Edit" to change the source code of your new worker
  9. Copy and paste the code from bot.js into the editor
  10. Add new file named telegram-bot.js and copy code from telegram-bot.js into the editor again
  11. Optional: Change the WEBHOOK variable to a different path. See https://core.telegram.org/bots/api#setwebhook
  12. Click on "Save and Deploy"
  13. In the middle panel append /registerWebhook to the url. For example: https://my-worker-123.username.workers.dev/registerWebhook
  14. Click "Send". In the right panel should appear Ok. If 401 Unauthorized appears, you may have used a wrong bot token.
  15. That's it, now you can send a text message to your Telegram bot

Setup through Cloudflare Wrangler(Recommanded if you know how to use NPM and commandline tools):

  1. open any shell and put command: npm create [email protected] "YOUR_NEW_PROJECT_FOLDER_PATH"
  2. Choose "Hello World" Worker for type of application
  3. Choose No for not to use TypeScript
  4. Choose if you like to use git for version control? No if you aren't sure
  5. Choose No for not to deploy yet
  6. Copy content of bot.js to overwrite all content of src/index.js
  7. Copy telegramBot.js to src/ folder
  8. add env variables to wrangler.toml like below
[vars]
ENV_BOT_TOKEN= "1234567890:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # bot token from [@BotFather](https://t.me/botfather)
ENV_BOT_SECRET= "ANY_RANDOM_WORDS" # See https://core.telegram.org/bots/api#setwebhook
  1. cd to YOUR_NEW_PROJECT_FOLDER_PATH and put command: npm run deploy
  2. command: curl "https://my-worker-123.username.workers.dev/registerWebhook". You can find your worker url at step 8.
  3. You should see Ok in step 9. If 401 Unauthorized/404 Not Fount appears, you may have used a wrong bot token.

Usage

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 bot.sendPlainText(message.chat.id, `Echo:\n${message.text}`);
}

example_inlineButtons.js

The file example_inlineButtons.js contains an improved bot, that demonstrates how to react to commands, send and receive inline buttons, and create MarkdownV2-formatted text.

example_inlineQueriesAndVoice.js

The file example_inlineQueriesAndVoice.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>"
    ],
  ]

example_reaction.js

The file example_reaction,js is a bot that randomly reacts to messages received. It demostrates how to use big reactions when the ๐ŸŽ‰ emoji gets chosen.

cf-telegram-bot's People

Contributors

cvzi avatar ibortolazzi avatar cojad avatar jwcheon avatar

Stargazers

Peyton Spencer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.