in ,

How to Using Telegram Bot To Send A Message With Python?

telegram bot python

In this lab, we will try to send a Telegram message to a private/public Group or channel as a part of a monitoring and updating system, using Telegram Bot and Python script.

Previously we did the same lab but using a PHP script, we created our Telegram Bot and used it to send Photos and Text messages including Links, to our private group or, of course, can target our public telegram Channel audiences.

Please Check Installing Python Virtualenv On Windows and A Simple Way To Installing And Run Python And PIP On Windows. if you were on Windows OS.

We will:

  1. Create A Simple Telegram Bot.
  2. Add The Telegram Bot To Our Channel or The Group AS An Admin.
  3. Coding Out Python Script To Send Our Message To The Targeted Recipients.
  4. Send Telegram Message With Photo, Text, And Link.

Create A Simple Telegram Bot

In the Telegram App (I use the telegram desktop app), search for @BotFather as shown in the below screenshot and select the verified signed BotFather one, which has a green right verification sign.

Telegram Bot BotFather

When you click on the @BotFather, the Telegram Bot Creator or Father :), it will be available to start creating a new Bot for you or modifying and managing your previous Bots.

Telegram Bot BotFather-Start

You will need to press the Start button to start creating our lab Bot:

From The Appearing interactive menu( your can type /help any time to show the interactive menu, and you can use the Menu button too):

Telegram Bot BotFather interactive menu

1- Press on /newbot (or chat with @BotFather with /newbot and press enter.)

Then the @BotFather will reply to you: Alright, a new bot. How are we going to call it? Please choose a name for your bot.

2- Enter our lab Bot name and username (the Bot username must end in ‘bot’).

I named the Bot: Linux Labs TTrigger Bot

And The Bot username: TTrigger_botTelegram Bot BotFather Create New Bot

 

All we need now is our Telegram Bot username (TTrigger_bot) and Token (5534844786:AAFsD-J4Vp_Iwyh3Gjnbg6XMWCxa77nFlJg), as shown in the output of the @BotFather message.

If you missed your Bot username/or token, you can talk any time to the @BotFather and ask him for /mybots, so select your Bot and select API Token.

Telegram Bot BotFather-show-bot-token

We will go to the next step and make our Bot an Admin for the target recipient Channel or Group.

Add The Telegram Bot To Our Channel or The Group AS An Admin.

We need to select Manage Channel/Group from our Channel or Group Menu.

Telegram-Manage-Group-Menu

And Select Administrators

Telegram-Manage-Group-Admins

Then the Add Administrator

Telegram-Manage-Group-Add-Administrator

Then Search and Select Our Telegram Bot TTrigger_Bot, to make it an admin to the Group/Channel. Then accept and save.

Telegram-Manage-Group-Add-Administrator-Accept

Coding Out Python Script To Send Our Message To The Targeted Recipients

The most straightforward Python script code to send a Telegram message using our Bot can be like the following (The Telegram Bot API can be found here).

If we need to send a photo + text (the text will be shown as the photo caption) including an URL to the Chat_id, which is our Gropu/Channel username.

#!/usr/bin/env python
# coding: utf-8

import requests

# Our Telegram Bot Token.
apiToken = "5534844786:AAFsD-J4Vp_Iwyh3Gjnbg6XMWCxa77nFlJg"
# Targeted Channel / Gropu Username.
chatID = "@GroupUserName"
# Inclduing link
link = "https://domain.com/file.html"
# Telegram Text Message.
textMessage = "This Photo message is sent using our Telegram Bot \n" + link
# The Photo URL
photoURL = "https://domain.com/url/to/photo.jpg"
# Telegram Bot API Send Request Format
apiReq = "https://api.telegram.org/bot"+apiToken+"/sendPhoto?chat_id="+chatID+"&caption="+textMessage+"&photo="+photoURL
# Fire The Telegram Message And Print The Result
print(requests.get(apiReq).json())

And if we need only to send a text message only, simply code as

#!/usr/bin/env python
# coding: utf-8

import requests

# Our Telegram Bot Token.
apiToken = "5534844786:AAFsD-J4Vp_Iwyh3Gjnbg6XMWCxa77nFlJg"
# Targeted Channel / Gropu Username.
chatID = "@GroupUserName"
# Inclduing link
link = "https://domain.com/file.html"
# Telegram Text Message.
textMessage = "This Photo message is sent using our Telegram Bot \n" + link
# Telegram Bot API Send Request Format
apiReq = "https://api.telegram.org/bot"+apiToken+"/sendMessage?chat_id="+chatID+"&text="+textMessage
# Fire The Telegram Message And Print The Result
print(requests.get(apiReq).json())

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

telegram bot php

How To Using Telegram Bot And PHP To Send A Message?

beautiful transformation before and after 021

21 Best Amazing Transformations In True Stories With Before And After Photos