Skip to content

Instantly share code, notes, and snippets.

@lucaspg96
Last active November 25, 2021 10:18
  • Star 26 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lucaspg96/284c9dbe01d05d0563fde8fbb00db220 to your computer and use it in GitHub Desktop.
A python script to send messages to a telegram user. Useful to notify when long scripts are finished!
#This script is independet of lib or python version (tested on python 2.7 and 3.5)
import telegram
#token that can be generated talking with @BotFather on telegram
my_token = ''
def send(msg, chat_id, token=my_token):
"""
Send a mensage to a telegram user specified on chatId
chat_id must be a number!
"""
bot = telegram.Bot(token=token)
bot.sendMessage(chat_id=chat_id, text=msg)
@rharyono725
Copy link

Hello - I'm pretty new to this Telegram Bot. I just had created my first bot through BotFather and I have my bot-token with me already. I have a question if I may ask you...

How do I obtain/get the target user's ChatID (my Telegram user's ID) to whom my bot is going to send the message, please?
Would the ChatID is dependent to my bot (meaning my ChatID must be provided by my Bot)?
Or it is tied to my Telegram User's ID (not numbers)?

Thanks. Rhar (Singapore).

@lucaspg96
Copy link
Author

Hi! No worry, some concepts are simple but I also take some time to understand then xD
So, answering your questions:

How do I obtain/get the target user's ChatID (my Telegram user's ID) to whom my bot is going to send the message, please?
There is a bot named @userinfobot that can give you this information. If you do not want to use it, you can create a dummy bot that receives a message and takes the chatId for each message. When the message comes from a private conversation, the chatId is the same as the userId.

Would the ChatID is dependent to my bot (meaning my ChatID must be provided by my Bot)? Or it is tied to my Telegram User's ID (not numbers)?
The chatId represents who I'm going to send this message, which can be a group (negative number), a user (positive number) and a channel (I think that can be the channel tag string). Different bots can use this same chatId numbers, since they have no relation to the bot itself.

Any questions, feel free to keep in touch :)

@rharyono725
Copy link

rharyono725 commented Oct 18, 2019

Hello Luc, Thanks for both responses - very helpful. The first #1 - I managed to get my Telegram UserID (a series of positive number). And the #2 above - I'm really interested to follow your bot.py and its revision. I will try to understand the revision tab - which lines I need to copy and which one I need to ignore - I guess, all the green lines are working and need to copy over. Thanks again. Rhar.

Added, can I know how to use your bot.py - it does not seem to have any main script, the lines are all part of "def" ... Should I call your bot.py from another Python script? Please help. Thanks.

@AllanM007
Copy link

Hi, can this script work by sending periodic messages to a telegram bot?

@lucaspg96
Copy link
Author

Hi Allan, this script only sends one message, but you can call this same function inside a script. For example, I have a script that checks the disk space of our server and notifies if some partition is becoming overloaded. I schedule this script to run every day using crontab on Ubuntu Server.

@Torento22
Copy link

Hello, i installed telegram module and i have an error in line 10
it says "module "telegram" has no "bot" member . ..
what's wrong?

@lucaspg96
Copy link
Author

Hi @Torento22, maybe the problem is the version. I know that they updated the API. This could be a breakchange...

@verata-veritatis
Copy link

verata-veritatis commented Mar 16, 2020

Hello, i installed telegram module and i have an error in line 10
it says "module "telegram" has no "bot" member . ..
what's wrong?

@Torento22 you should be pip-installing python-telegram-bot package, not telegram.

@Klvxn
Copy link

Klvxn commented Oct 16, 2020

Hi Allan, this script only sends one message, but you can call this same function inside a script. For example, I have a script that checks the disk space of our server and notifies if some partition is becoming overloaded. I schedule this script to run every day using crontab on Ubuntu Server.

I want to write to python script that sends periodic message to a telegram bot. How can I do it?

@lucaspg96
Copy link
Author

Hi Allan, this script only sends one message, but you can call this same function inside a script. For example, I have a script that checks the disk space of our server and notifies if some partition is becoming overloaded. I schedule this script to run every day using crontab on Ubuntu Server.

I want to write to python script that sends periodic message to a telegram bot. How can I do it?

Hi @greazee,how I said before, the way I did was using crontab at my Ubuntu Server. I scheduled to execute the script each 2 hours and, if the memory/cpu usage surpasses a threshold (80%), the script send a message to my telegram using a telegram bot

@hamdan-Deb
Copy link

import Schedule

@Yakov-Bader
Copy link

could i send a massage from my private account, and not from my bot?

@esakkiraja100116
Copy link

could i send a massage from my private account, and not from my bot?

there is any way ?
see this :
https://www.youtube.com/watch?v=ULIdgdR84Gg

@exec85
Copy link

exec85 commented Jul 9, 2021

I am also very new to this, lemme explain my situation:

I want to share my tool (selenium python app with a tkinter GUI) with some friends or unknown persons.
I would like to let telegrambot send a message like "Tool started" once my app gets started.

How can I achieve it that every user, using my app, is geting a private message?

@exec85
Copy link

exec85 commented Jul 9, 2021

_How do I obtain/get the target user's ChatID (my Telegram user's ID) to whom my bot is going to send the message, please?
If you do not want to use it, you can create a dummy bot that receives a message and takes the chatId for each message.

Any advice fora code that would achieve that? I am struggling woth that part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment