I've dabbled with Telegram in the past and heard good things, so I thought I'd give it another go. The API is a lot more open than the previous instant messenger I was using (Google Hangouts) so I wondered to how 'hackable' it was. After some research I stumbled upon the ability to send notifications from Tasker (A 'scripting' type app for Android) to Telegram
Click 'read more' to see how to get Tasker to send notifications to Telegram
It's assumed you already have Telegram and Tasker installed, and you have some idea of how to use both. The instructions below are for getting things setup on Android. As Tasker is only available on Android, this shouldn't cause any issues.
Setting up 'BotFather'
BotFather is a tool for setting up your own bot. First of all you will need to send the bot a message by sending a message to @BotFather and then pressing start, which will then reply with a list of instructions-
Then enter the following to create the new bot-
/newbot
You will then get prompted to set the name. You can set it to anything-
You will then get promoted to set the username of your bot. It must be unique. You will then get some helpful information, and the API key. You'll need this!-
Creating a channel for notifications
Your bot can only send notifications to a channel. Initially you must configure it to be 'Public' but we can change this later.
Create the channel by clicking on the 'settings' icon in the top right corner on your home page, and then selecting 'New Channel'. You will then be prompted to enter a name, and optional description. Click the tick, and then select 'Public Channel' and enter a suitable link-
You will then get prompted to add members. Skip this and go to your newly created channel. Click the channel name at the top and then Administrators, and search for your newly created Bot. Select the bot, and click 'Ok' when prompted if you want to make the Bot an Administrator-
Testing the bot
It's possible to check the bot is working using curl. The format is below-
curl -i -X GET "https://api.telegram.org/bot[YourKey]/sendMessage?chat_id=[@YourChannelName]&text=[YourText]"
For my example this should look like the below-
curl -i -X GET "https://api.telegram.org/bot336112886:AAH09ZnJx_MQ7-ujt2gKhLtBB0gMbxyiexU/sendMessage?chat_id=@Test2356786532110976&text=test"
Which should return the following. Make a note of the 'id' including the hypen if there is one-
HTTP/1.1 200 OK Server: nginx/1.10.0 Date: Tue, 04 Apr 2017 19:58:54 GMT Content-Type: application/json Content-Length: 180 Connection: keep-alive Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, POST, OPTIONS Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection Strict-Transport-Security: max-age=31536000; includeSubdomains {"ok":true,"result":{"message_id":2,"chat":{"id":-1001102920987,"title":"Test2356786532110976","username":"Test2356786532110976","type":"channel"},"date":1491335933,"text":"test"}}
And you should see the message in Telegram-
Making the channel private
Now you have the chat id, we can make the channel private, by clicking the 'settings' icon in the top right corner, then edit > channel type > private -
Now retest the bot with curl, this time using a chat id instead of the channel name-
curl -i -X GET "https://api.telegram.org/bot336112886:AAH09ZnJx_MQ7-ujt2gKhLtBB0gMbxyiexU/sendMessage?chat_id=-1001102920987&text=test2"
You should see the message appear in the now private channel-
Sending messages from Tasker to Telegram
Add a new Task and give it a Name-
Then add an 'action' and click on 'Variables' > 'Variable Set'
Fill in the action as per below-
Name-
%TeleMsg
To-
Test
It should then look something like this-
Then add a new action, click 'Net', and then 'HTTP Post'-
Fill it in with the details below, remembering to update your Key and Channel ID-
Server:Port-
https://api.telegram.org
Path-
/bot[YourKey]/sendMessage?chat_id=[YourChanneID]&text=%TeleMsg
Content Type-
application/x-www-form-urlencoded
Once the task is completed you should have something that resembles the following-
You can then press the 'Play' button in the bottom left corner to test your Telegram message.
And that's all there is too it. You can now add custom Tasker profiles to trigger notifications to Telegram. I use them to let my Wife know when I'm on my way home, or when my mobile battery is running out, but I'm sure I will think of more uses in the future
Thanks for this overview - did you get it working with sendPhoto? I failed with this ... :-(
ReplyDeleteI've not tried it sorry, but good luck!
ReplyDeleteTry Remote Bot for Telegram. It has Tasker plugin for sending messages from Tasker to Telegram.
ReplyDeleteI have a problem
ReplyDeleteCommand send thru tasker do not work (es: /gpio 21 on)
The same command directly on telegram channel work.
is something wrong in string "application/x-www-form-urlencoded"
Thank