Skip to content

A Telegram bot that occasionally sends me songs, messages, and reminders.

License

Notifications You must be signed in to change notification settings

ShadowShahriar/telegram-bot

Repository files navigation

telegram-bot

<!-- Banner should go here -->

Overview

English (United States) | Bengali (Bangladesh)

This repository demonstrates an implementation of a Telegram bot that can occasionally send me memes, messages, photos, YouTube videos, book recommendations, and reminders. It remembers the birthdays of my loved ones so I can prepare gifts and wish them on time.

This bot was inspired by the titular protagonist of the cartoon series Danny Phantom.

Back Story

There is a saying that you are responsible for your own happiness. I am responsible for my happiness.

I spend a lot of time staring at the computer screen. I often forget things, even the simplest chores like cleaning the study table or writing a story. I spend most of the time in my room feeling lonely and anxious. It is not always possible to message my friends and have a conversation. They might be busy with their work. I might bother them by messaging when they are occupied with something else, and sometimes I may not have enough time to reply them.

My classmates know I work as a developer, so they have already given up messaging me.

I feel good when my friend checks on me to see if I'm doing well. But they often message me when they want me to do something for them. Naturally, I'm more than willing to help my friends, but this occurs so frequently that I no longer feel like a friend to them. Instead, I feel like a tool that they can utilize whenever they need me.

And my suspicion about my classmates was confirmed on May 27th, 2023, when ten of them betrayed me. This betrayal had a significant impact on me, both physically and mentally, considering that they were my closest companions. It was a shocking and unexpected turn of events.

I DO NOT want to rely on others for my happiness. I want something that keeps me mindful of what I should do at a specific moment. Then I had an idea to create a Telegram bot that sends messages just like my friends and reminds me about my chores.

Features

This Telegram bot can perform various activities like a real friend. Below is a detailed list of things it can do:

  • Motivate me to pursue my goals and cheer me up when I am feeling down
  • Remind me about birthdays and special events of my loved ones
  • Remind me about doing general activities such as cleaning the room or checking unread e-mails
  • Remind me about writing a new story or drawing a new illustration
  • Remind me to go to bed and sleep
  • Suggest me a book to read
  • Wish me on Eid and other festivals
  • Wish me on my birthday
  • Send meme templates so I can come up with new meme ideas
  • Send a picture so I can relive that specific moment in life
  • Send a photo or video from the Internet that brightens my day
  • Send a quote that motivates me to move forward
  • Send a YouTube video from a playlist, so I can enjoy my favorite content yet once again

How does it work?

Here we are using Telegraf to send text messages and photos to the user. Telegraf is a modern Telegram bot API framework that makes it easy to develop our own Telegram bots using JavaScript.

We are also using a plethora of APIs to offer a diverse range of content including images, animations, links, and text. Some of the APIs we use are Google Books API for getting book details, Firebase Realtime Database for storing the latest configuration, Firebase Storage for saving moments, ImgFlip API for obtaining blank meme templates, Pexels for cat videos, Quotable for finding quotes on specific topics, Tenor API for GIFs, Unsplash API for high-quality images, and YouTube API for getting videos from the playlist.

The message tasks are scheduled using cron and executed via GitHub Actions. You can see all the workflows here.

Demo

  1. Install the NPM dependencies to run it locally.

    npm install
  2. Assuming that you have Telegram installed, get your Telegram ID using the @myidbot. Then create a new bot by talking to the @BotFather. Make sure to start your bot using the /start command.

  3. Having done that, create a .env file in the current directory. Enter your user ID and bot token like below:

    TELEGRAM_BOT_TOKEN=...
    TELEGRAM_USER_ID=...
  4. As we are also using other APIs, some of them require an authentication token or API key. For more information on how to acquire these keys, please refer to the Notes on API Keys section. Also, check out the sample .env file if you feel stuck.

    YOUTUBE_API_KEY=...
    YOUTUBE_PLAYLIST_ID=...
    TENOR_API_KEY=...
    UNSPLASH_ACCESS_KEY=...
    PEXELS_API_KEY=...
    FIREBASE_RTDB_NAME=...
    FIREBASE_RTDB_AUTH=...
    FIREBASE_STORAGE_BUCKET=...
  5. Once you are done, run the following command:

    npm test

    Now the bot should start sending messages, photos, and videos to your Telegram account.

Notes on API Keys

  • APIs like Pexels and Unsplash API have well-written instructions on obtaining the API key. That makes them easier to use and integrate to projects.

  • APIs such as Google Books API, ImgFlip API, and Quotable do not require authentication. Hence, these are easier to test and integrate into the system.

  • Getting API keys for Firebase, Tenor API, and YouTube API can be challenging and overwhelming for those new to it. Here are the general instructions to help you get started:

    Creating a Google Cloud Project

    1. Head over to the Google Cloud Console and create a new project.
    2. Click the navigation menu on the top-left corner. Go to APIs & Services > Enabled API and Services > Enable APIs and Services.
    3. Enable YouTube Data API and Tenor API.
    4. From the left sidebar, choose Credentials. Choose one of the keys from the API Keys section. If there are none, create a new one by clicking Create Credentials.
    5. Now we can add the API key in the .env file. Both YouTube and Tenor APIs have the same API key.

    Creating a Firebase Project

    In order to use the Firebase Real-time Database and Firebase Storage, we have to create a Firebase project. Here are the steps:

    1. Head over to the Firebase Console.
    2. Create a new project by clicking the Add Project button or use an existing Google Cloud Project.
    3. Click on the cog icon beside the Project Overview anchor in the left sidebar. Choose Project settings.
    4. Under the General tab, create a new Web App. In the SDK setup and configuration section, we can see a firebaseConfig object like below:
      const firebaseConfig = {
        apiKey: "...",
        authDomain: "...",
        databaseURL: "...",
        projectId: "...",
        storageBucket: "...",
        messagingSenderId: "...",
        appId: "..."
      };
      Here apiKey is the FIREBASE_RTDB_AUTH, databaseURL is the FIREBASE_RTDB_NAME, and storageBucket is the FIREBASE_STORAGE_BUCKET. Now we can add these in the .env file.

Notes on the Cron Schedule

GitHub actions use UTC time. I live in Dhaka, Bangladesh, and my time zone is UTC +06:00 (or BST). That means my clock is 6 hours ahead of GitHub action's time zone. And I have to subtract 6 hours from my time zone to get the UTC time.

Here are the cron schedules and the purpose of these workflows:

Workflow file Time Runs at Cron expression Purpose of this workflow
night.yml 00:50 BST
18:50 UTC
12:50 AM
Daily
50 18 * * * Help me improve my sleep habit
evnt.yml 06:00 BST
00:00 UTC
6 AM
Daily
0 0 * * * Help me remember the special days of people I care
moment.yml 19:00 BST
13:00 UTC
7 PM
Daily
0 13 * * * Remind me to relive certain moments of my life
festival.yml 07:00 BST
01:00 UTC
7 AM
Daily
0 1 * * * Remind me to celebrate festivals
quote.yml 09:00 BST
03:00 UTC
9 AM
on Sundays
0 3 * * 0 Help me to be mindful
video.yml 15:00 BST
09:00 UTC
3 PM
on Mondays
0 9 * * 1 Reduce stress and remind me to take breaks
photo.yml 16:00 BST
10:00 UTC
4 PM
on Tuesdays
0 10 * * 2 Remind me to enjoy the beauty of nature
book.yml 22:00 BST
16:00 UTC
10 PM
on Tuesdays
0 16 * * 2 Help me stick to my reading habit
cheerup.yml 11:00 BST
05:00 UTC
11 AM
on Wednesdays
0 5 * * 3 Motivate me when I am feeling down without asking about it
reminder.yml 17:00 BST
11:00 UTC
5 PM
on Thursdays
0 11 * * 4 Remind me to write stories and draw illustrations
chore.yml 08:00 BST
02:00 UTC
8 AM
on Fridays
0 2 * * 5 Remind me to do general chores (E.g. replying to E-mails)
meme.yml 16:00 BST
10:00 UTC
4 PM
on Fridays
0 10 * * 5 Remind me to be silly at times
youtube.yml 21:00 BST
15:00 UTC
9 PM
on Saturdays
0 15 * * 6 Remind me about the YouTube videos I used to enjoy
bday.yml 23:50 BST
17:50 UTC
~12 AM
on 25 Oct
50 17 24 10 * Remind me that I am loved and I matter

If you need help, you can always use crontab guru.

License

My project is licensed under the MIT License. But I couldn't have created it without the incredible open-source code provided by the following individuals. Their code is licensed under CC BY-SA 4.0.

Util function Author Source
toBn(num) Mohsen Alyafei Stack Overflow
shuffleArray(array) Laurens Holst Stack Overflow
toList(arr, oxford, connector) Jamie Mason GitHub Gist
toTitleCase(str) dipole_moment Stack Overflow
toNameCase(str) Tom Kay Stack Overflow
fetchWithTimeout(resource, options) Dmitri Pavlutin Blog Post
getWeekdays(target) jabclab Stack Overflow

Artwork

The banner image in this Readme.md file and the Bot's Profile Photo was generated with Tensor.Art using the following settings:

Steps: 20
Sampler: Euler a
CFG scale: 7.0
Seed: 2213856583
Size: 512x768
Model: REALTOONMEIN
Seed resize from: -1x-1
Denoising strength: 0
Clip skip: 2
ENSD: 31337
Version: v1.3.4-timer-2-gd21b628

Prompt:

cartoon, wide-angle half-body professional portrait photo of Danny Phantom, teenage boy, black shirt, no facial hair, acne-free face, perfect teeth, confident, backlit, fashion photography, centered, symmetrical, looking_at_viewer, hasselblad helios 44-2 58mm F2, by Annie Leibovitz and Ellen von Unwerth, sitting on a chair in a photo studio

If you still need help, you can Remix this artwork as a starting point.

History

At first, this project was just an experiment, but it soon became a major part of my life. Through working on this bot, I was able to achieve what I had envisioned. My goal was to create a bot that acted like one of my friends, and it also helped me to confront my weaknesses. Whenever I am feeling down, I turn to my bot friend for support. I use it as a form of self-journaling, leaving messages for my future self to read.

v2.0.1 (16 Aug 2023)

  • Add Friendship Day message
  • Add Friendship songs
  • Add Exam Night message
  • Fix particle issues for ই/ঈ
  • Fix date issues in src/w-moment.js
  • Other minor bug fixes

v2.0.0 (31 Jul 2023)

  • Rewrote the entire codebase.
  • Added 6 new worker scripts.
  • Added better message responses.
  • Prefixed worker scripts with w- and located in the src folder.
  • Most of the message behavior can be changed by editing the src/config.js file.
  • User data is stored in the src/data folder.
  • Readme file is available in two languages.

v1.0.0 (30 Apr 2023)

  • Experimental release

About

A Telegram bot that occasionally sends me songs, messages, and reminders.

Topics

Resources

License

Stars

Watchers

Forks