How to make a simple telegram bot on python

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

It's very simple to make a telegram bot using python. Let's try and make a simple bot - it has only one supported command /cat (to get random cat pictures) apart from the default /start and /help.
Requirements -
pip3 install python-telegram-bot --upgrade
pip3 install requests
We will need to make a main.py file, below is a scaffold which we will need for almost any bot -
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def start(update, context):
update.message.reply_text('Hi use /cat to get cat pics!')
def help_command(update, context):
update.message.reply_text('Help: use /cat to get cat pics!')
if __name__ == '__main__':
TOKEN = "<the bot token>"
updater = Updater(TOKEN, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help_command))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C
updater.idle()
In the above code, we need to add a new handler inside __main__
dp.add_handler(CommandHandler("cat", catpic))
Now, we need to define the catpic handler method
def catpic(update, context):
import requests
url = requests.get('https://api.thecatapi.com/v1/images/search').json()[0]["url"]
update.message.reply_photo(url)
update.message.reply_text("Here is a nice cat!")
We get a image url from the API and use the reply_photo method to send that image link.
/start to see a list of all commands/newbot to start creation of a new botTOKEN variable with the new bot tokenpython3 main.py
Goto your bot on telegram and try the commands -
/start/help/cat/cat command should give you a nice picture of a cat.