Bring Peace to Your Telegram Channel with ZenQuotes and Python

Are you a Telegram channel admin looking to inspire your users with ZenQuotes? In this tutorial, we’ll show you how to build a Telegram bot that sends a random entry from our API to your Telegram channel after every 25 messages.

Very little coding experience is necessary to get started, as we’ll be using Python and the Telegram Bot API to create the program. By the end of this tutorial, you’ll have a fully functional bot that will help you spread inspiration to your channel’s followers. Let’s get started!

Basic Requirements

  1. Create a Telegram bot using BotFather and obtain the API token.
  2. Install the necessary libraries, such as python-telegram-bot for interacting with the Telegram API and requests for making HTTP requests to the ZenQuotes API.
  3. Set up a counter to keep track of the number of messages sent by the bot.
  4. Use the python-telegram-bot library to handle incoming messages to the bot.
  5. After every 25 messages sent, make a request to the ZenQuotes API to fetch a random quote.
  6. Send the random ZenQuotes entry to the Telegram chat.

Obtain a Telegram API Key

BotFather is a Telegram Bot that allows you to create, manage and edit your own Telegram Bots. It’s an official Telegram Bot and it’s very easy to use. You can access it by searching for “@BotFather” in the Telegram app.

Once you start a chat with BotFather, you can use various commands to create and manage your bots. Some of the common commands include:

  • /newbot – create a new bot.
  • /setname – set the name of your bot.
  • /setdescription – set the description of your bot.
  • /setcommands – set the list of commands that your bot supports.
  • /token – get the token for your bot, which is required to interact with the Telegram API.

Using BotFather, you can also manage other aspects of your bot, such as its profile picture, privacy settings, and more.

Build the Bot

Once you have obtained your API key from BotFather, you can use the following code as a template for the ZenQuotes bot:

Python

import random
import requests
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# Replace YOUR_BOT_TOKEN with the token obtained from BotFather
bot_token = 'YOUR_BOT_TOKEN'

# Create an instance of the Updater class
updater = Updater(bot_token)

# Define a counter variable
counter = 0

# Define a function to handle incoming messages
def handle_message(update, context):
    global counter
    counter += 1
    
    # Send a random ZenQuote after every 25 messages or customize
    if counter % 25 == 0:
        # Make a request to the ZenQuotes API
        response = requests.get('https://zenquotes.io/api/random')
        data = response.json()[0]
        quote = data['q'] + ' - ' + data['a']
        
        # Send the quote to the chat
        context.bot.send_message(chat_id=update.effective_chat.id, text=quote)

# Set up a MessageHandler to handle incoming messages
message_handler = MessageHandler(Filters.text & (~Filters.command), handle_message)
updater.dispatcher.add_handler(message_handler)

# Start the bot
updater.start_polling()

Remember to replace YOUR_BOT_TOKEN with the token obtained from BotFather. Note that you may need to make modifications and add error handling as needed to suit your specific use case.

Starting and Stopping

To stop the bot you created, simply stop the Python program that is running the bot. If you started the bot using the updater.start_polling() method, you can stop the bot by calling the updater.stop() method.

About Updater

The Updater class is a key component of the python-telegram-bot library, which is a Python wrapper for the Telegram Bot API. The Updater class is responsible for receiving updates from the Telegram API and sending them to the appropriate handlers for processing.

Here’s a brief overview of how the Updater class works:

  • The Updater class is initialized with a bot token, which is obtained from the BotFather when you create your bot.
  • The Updater class creates a connection to the Telegram API using the provided token.
  • The Updater class starts a background thread to continuously fetch updates from the Telegram API.
  • When an update is received, the Updater class parses the update and sends it to the appropriate handler for processing.
  • In your bot code, you can use the Updater class to create an instance of the bot and start listening for updates from the Telegram API. You can also add handlers to the Updater instance to handle different types of updates, such as text messages, photos, or commands.

Subscribe