Sunday, July 22, 2018
Send photos with Telegram
Send photos with Telegram
In the last post we saw how to use the Telegram bot APIs to send a message from a Raspberry Pi to our smartphone.
This time I will show you how easy is sending a photo using the same APIs and the Python library. You can use the same APIs also for sending videos and audio files with really small efforts.
First of all be sure to have the library installed. If you need help on installing it, just see the previous post or go directly to the library site.
Anyway note that on some system you may need to install another library before. This usually happens if you are using Python 2.x instead of Python 3.x for coding your scripts.
If the Telegram library gives an error when trying to install, just install this one before and then retry:
sudo pip install future
Now lets see the code for sending an image:
import telegram
import picamera
# Connect to our bot
bot = telegram.Bot(token="123456789:ABCDabcdefgYGscai2d_hakusnci7Hdks")
# Sets the id for the active chat
chat_id=123456789
#Get the photo
camera=picamera.PiCamera()
camera.capture(./capture.jpg)
camera.close()
# Sends a message to the chat
bot.sendPhoto(chat_id=chat_id, photo=open(./capture.jpg, rb))
Again this is a really simple script. Lets see how it works...
At the beginning we just import the Telegram and the PiCamera libraries. Then the bot is created like we did in the previuos post.
After this we need the chat id. Remember that a bot cannot start a chat by itself, so you need to send it a message from your smartphone to create the chat and be able to retrieve its id. Then you can just store this number into a file or write it down to use it direcly in your script as I did above (the chat id used above is of course a fake number).
Now we need to get an image. You can use also different formats and you can get it from other sources. In this example I just get a snapshot from the PiCamera.
The last line is the one that actually sends the image. You need to specify the chat id and the file obiect (created with the open() standard function).
You can also send an image from the web by using the following syntax for the last line:
At the beginning we just import the Telegram and the PiCamera libraries. Then the bot is created like we did in the previuos post.
After this we need the chat id. Remember that a bot cannot start a chat by itself, so you need to send it a message from your smartphone to create the chat and be able to retrieve its id. Then you can just store this number into a file or write it down to use it direcly in your script as I did above (the chat id used above is of course a fake number).
Now we need to get an image. You can use also different formats and you can get it from other sources. In this example I just get a snapshot from the PiCamera.
The last line is the one that actually sends the image. You need to specify the chat id and the file obiect (created with the open() standard function).
You can also send an image from the web by using the following syntax for the last line:
bot.sendPhoto(chat_id=chat_id, photo="http://www.thingiverse.com/img/thingiverse-logo-2015.png")
just use the image link instead of the open() function and you are done.
As I wrote above, you can also send different type of files. You only need to use the right function. For example you can use
sendAudio() to send an audio file
sendVideo() to send a video clip
sendDocument() to send a generic file
and so on. Take a look at the telegram library docs to find all the supported functions.
As you can see, Telegram is much easier to use from a Raspberry Pi than Yowsup, so if you need to communicate with your device I really suggest you to use this software.
As I wrote above, you can also send different type of files. You only need to use the right function. For example you can use
sendAudio() to send an audio file
sendVideo() to send a video clip
sendDocument() to send a generic file
and so on. Take a look at the telegram library docs to find all the supported functions.
As you can see, Telegram is much easier to use from a Raspberry Pi than Yowsup, so if you need to communicate with your device I really suggest you to use this software.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.