Matthias Kauer
Matthias Kauer

Reputation: 10437

Sending Telegram message from Python

I want to send a message from a Python script via Telegram. I have tried to do that via telegram-cli, both the original version from vysheng and the patched version from luckydonald. With both of them I could successfully send messages to my phone. My problem is that:

So my question is: Should I go back to the older pytg? Can I fix the shell scripts or amend them to Python by inputting a stringIO from subprocess.call or popen? Is anyone out there using this in a robust fashion?

Background

Upvotes: 17

Views: 56458

Answers (3)

kirelagin
kirelagin

Reputation: 13606

  1. Talk to @BotFather. Send /newbot to them and follow the prompts. In response you will get an HTTP API token (it will look something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11).

  2. Talk to your newly created bot, it is enough to just /start it.

  3. Immediately open https://api.telegram.org/bot<token>/getUpdates?offset=-1 in your web-browser (literally paste the token you received from the BotFather, complete with all the letters and punctuation). Copy the chat id from the returned JSON object.

  4. With the data above, sending a message is a matter of a simple POST request.

    For example, using the Requests library:

    import requests
    
    TOKEN = '...'
    CHAT_ID = '...'
    SEND_URL = f'https://api.telegram.org/bot{TOKEN}/sendMessage'
    
    requests.post(SEND_URL, json={'chat_id': CHAT_ID, 'text': your_message})  
    

Upvotes: 3

juanmah
juanmah

Reputation: 1189

The first step is to create a bot and get the token.

The second step is go get the chat_id:

  • Write something in the chat
  • Visit https://api.telegram.org/bot<YourBOTToken>/getUpdates and get the chat_id under the key message['chat']['id'].

The last step is to use this code:

import requests

def telegram_bot_sendtext(bot_message):

   bot_token = ''
   bot_chatID = ''
   send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

   response = requests.get(send_text)

   return response.json()


test = telegram_bot_sendtext("Testing Telegram bot")
print(test)

Code extracted from Medium.com: How to create a Telegram bot, and send messages with Python

Upvotes: 49

ebeneditos
ebeneditos

Reputation: 2612

I'd rather use the package python-telegram-bot, it works well for me.

You can find here documentation and an easy example to get started.

In order to reply to text messages, you can add a MessageHandler after the CommandHandler such as:

updater.dispatcher.add_handler(MessageHandler(Filters.text, text_reply))

def text_reply(bot, updater):
    text = update.message.text

    if text == 'ping':
        reply = 'pong'

    elif text == 'pong':
        reply = 'ping'

    # add any process to the text

    else:
        reply = "I only respond to ping pong"

    update.message.reply_text(reply)

Don't forget to import from telegram.ext import MessageHandler.

Hope this helped!

Upvotes: 2

Related Questions