• 3

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Sending messages with Telegram - APIs or CLI?

I would like to be able to send a message to a group chat in Telegram. I want to run a python script (which makes some operations that already works) and then, if some parameters have some values the script should send a message to a group chat through Telegram. I am using Ubuntu, and Python 2.7

I think, if I am not wrong, that I have two ways to do that:

  • Way One: make the Python script connect to the Telegram APIs directly and send the message (https://core.telegram.org/api).

  • Way Two: make the Python script call the Telegram's CLI (https://github.com/vysheng/tg), pass some values to this and then the message is sent by the Telegram's CLI.

I think that the first way is longer, so a good idea might be using the Way Two.

In this case I really don't know how to proceed. I don't know lots about scripts in linux, but I tried to do this:

cd /home/username/tg
echo "msg user#******** messagehere" | ./telegram
sleep 10
echo "quit" | ./telegram

this works at a half: it sends the message correctly, but then the process remains open. And second problem, I have no clue on how to call that from python and how to pass some value to this script. The value that I would like to pass to the script is the "messagehere" var: this would be a 100/200 characters message, defined from inside the python script.

Does anyone has any clues on that? Thanks for replies, I hope this might be useful for someone else.

Telegram recently released their new Bot API which makes sending/receiving messages trivial. I suggest you also take a look at that and see if it fits your needs, it beats wrapping the client library or integrating with their MTProto API.

import urllib
import urllib2

# Generate a bot ID here: https://core.telegram.org/bots#botfather
bot_id = "{YOUR_BOT_ID}"

# Request latest messages
result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/getUpdates").read()
print result

# Send a message to a chat room (chat room ID retrieved from getUpdates)
result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/sendMessage", urllib.urlencode({ "chat_id": 0, "text": 'my message' })).read()
print result

Unfortunately I haven't seen any Python libraries you can interact directly with, but here is a NodeJS equivalent I worked on for reference.

  • 30
Reply Report
      • 1
    • This link may answer the question, but can you provide a little more context than just linking to the API? Maybe a small snippet on how to set up API and send messages?
      • 1
    • I updated the answer with some Python examples. I worked with the API in NodeJS, but the sample code should work fine.
      • 1
    • thank you very much! I am now using their Bot API through github.com/leonjza/hogar and it is much much simpler!

Since version 1.05 you can use the -P option to accept messages from a socket, which is a third option to solve your problem. Sorry that it is not really the answer to your question, but I am not able to comment your question because I do not have enough reputation.

  • 10
Reply Report

First create a bash script for telegram called tg.sh:

cd ${tgpath}
${tgpath}/telegram -k ${tgpath}/tg-server.pub -W <<EOF
msg $to $subject
echo "$now Recipient=$to Message=$subject" >> ${LOGFILE}
echo "Finished" >> ${LOGFILE}

Then put the script in the same folder than your python script, and give it +x permission with chmod +x tg.sh

And finally from python, you can do:

import subprocess
subprocess.call(["./tg.sh", "user#****", "message here"])
  • 5
Reply Report
      • 2
    • it's not work and this error accorded: [warn] Epoll ADD(1) on fd 0 failed. Old events were 0; read change was 1 (add); write change was 0 (none): Operation not permitted

You can use safe_quit to terminate the connection instead since it waits until everything is done before closing the connection and termination the application

cd /home/username/tg
echo "msg user#******** messagehere\nsafe_quit\n" | ./telegram

use this as a simple script and call it from python code as the other answer suggested.

  • 2
Reply Report

I would recommend the first option.

Once you are comfortable with generating an AuthKey, you should start to get a handle on the documentation.

To help, I have written a detailed step-by step guide of how I wrote the AuthKey generation code from scratch here.

It's in vb.net, but the steps should help you do same in python.

  • 0
Reply Report

Trending Tags