Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

#!/bin/bash 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.

like image 724
Michele Avatar asked Jun 20 '14 15:06

Michele


People also ask

Can we use Telegram as an API?

We offer two kinds of APIs for developers. The Bot API allows you to easily create programs that use Telegram messages for an interface. The Telegram API and TDLib allow you to build your own customized Telegram clients.

What can I do with Telegram API?

This is the API used by Telegram apps for all your actions on Telegram. To name a few: viewing your chats, sending and receiving messages, changing your display picture or creating new groups. Through the Telegram API you can do anything you can do in a Telegram app programatically.

Can Telegram BOT send messages?

It is only possible to send messages to users whom have already used /start on your bot. When they start your bot, you can find update. message.

What is CLI Telegram bot?

Telegram CLI is basically full-blown Telegram client, like the Telegram app on your phone or laptop, that works in the command-line instead of with a GUI. This article contains a collection of useful information and commands for working with Telegram CLI.


1 Answers

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.

like image 174
Chris Brand Avatar answered Sep 23 '22 12:09

Chris Brand