Skip to content

Free serverless Telegram bot, powered by a Cloudflare Worker

Notifications You must be signed in to change notification settings

freezerain/telegram-bot

Repository files navigation

Alfred - Telegram Butler Bot 🤖

Free serverless Telegram bot, powered by a Cloudflare Worker, is designed to automate tasks in group chat. Originally was created to daily poll 4 members of the party if they ready to go in the adventure.

Demo 🚀

Ask chatGpt directly from telegram

Demo GIF

Dog on demand as service 🐕

Demo GIF

Random gif 😻

Demo GIF

Access to telegram API

Demo GIF

What Can This Bot Do and what APIs supported 🛠️:

  • OpenAI API: Ask ChatGPT 3.5 / 4. This requires openAI token
  • EpicGames API: Get updates on free games from the Epic Games Store.
  • Giphy API: get random gif or specific tag
  • DogCEO API: get random Dog Photo
  • Chuck Norris API: joke about Chuck Norris
  • Telegram API: Poll Creation from message or code
  • Telegram API: Dice roll for random integer

You can schedule these actions using Cloudflare's cron timer. The bot also uses Cloudflare's D1 serverless SQL database for data storage.

How to Set It Up ⚙️

1. Telegram 📱

Follow Telegram Bot Tutorial to:

  1. Create a bot using @BotFather and save its token to secret TELEGRAM_BOT_TOKEN.

Optional:

  • Using @BotFather disable group privacy to let the bot see all messages.
  • Using @BotFather register keywords as commands for rendering in Telegram after "/".

2. Cloudflare Dashboard ☁️

  1. Create a worker.
  2. Set worker link as Telegram webhook. https://api.telegram.org/bot{my_bot_token}/setWebhook?url={url_to_send_updates_to}
  3. Create new D1 database and epicgames table with id (primary key int), chatid (string), and games (string).
  4. Create a new AI gateway for openAi.

Optional:

  • Create new token and save it to GitHub secrets if you want to deploy using GitHub actions.

3. Your Code 💻

  1. In src/res/config.js, set aiGateway and epicTableName that you get from cloudflare dashboard.
  2. In wrangler.toml, set worker name, D1 database_name and database_id.

Optional:

  • To schedule actions: create desired cron timers in cloudflare dashboard, put them in src/res/crons.js, and link them to the routine in src/action/scheduledMap.js.
  • To use different keywords or language: change keywords in src/res/keywords.js.
  • To add new APIs/Actions:: Put new actions in src/action/actions and register them in src/action/actionMap.js or src/action/scheduledMap.js for cron routines.

4. Secrets 🔐

Store secrets in the GitHub repo for deployment through GitHub Actions. They'll be pushed to the Cloudflare dashboard. Alternatively, add secrets directly in the dashboard:

  • TELEGRAM_BOT_TOKEN: To send data back in the Telegram chat.

Optional:

  • CLOUDFLARE_API_TOKEN: For bot deployment from GitHub actions
  • GIPHY_API_KEY: To search for GIFs.
  • OPENAI_API_KEY: To chat with ChatGPT.

Contribute 🤝

Feel free to use, change, and share this code as you like. No license attached. Pull requests are welcomed.

Heads Up ⚠️

This project isn't officially associated with Cloudflare, Telegram, Epic Games, OpenAI, or Giphy. It's a standalone project, created for learning and fun, and comes as it is.

About

Free serverless Telegram bot, powered by a Cloudflare Worker

Resources

Stars

Watchers

Forks