Telepot is a python based telegram bot framework which is lightweight, easy to use and supports all of the up-to-date telegram features. For example, I was able to write a small telegram bot running on Pi2 which takes and sends a photo with only few lines using it. Here is how.
1) Although Telepot supports python 2.7 without async feature, I wanted to use Python 3.x. However, the latest Telepot supports async requires Python 3.5 so I had to download and build Python 3.5 on my Pi2. I followed the instruction introduced here https://sowingseasons.com/blog/software/2016/01/building-python-3-4-on-raspberry-pi-2/19864079 except using python source https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
2) Telepot also provides basic samples. You can also find out other API usages through test code. ‘couter/countera’ example is simple but a good start points if you only require receiving telegram message and sending a reply back.
3) Of course you’ll need a Telegram Bot account. There’s a bot named ‘@BotFather’ which does making a bot for you and makes you setting basic info of it. Just initiate a talk to the account and detailed instruction described here: https://core.telegram.org/bots
4) I used the https://github.com/nickoala/telepot/blob/master/examples/chat/countera.py as the starting point. From the source, MessageCounter.on_chat_message will be called responds to the user’s message. If you want to make the CCTV like telegram bot, then just take a picture and send it back within the method.
5) Takes a picture using the picamera module and replies with it. Remove the temporary saved picture after sending it.
6) here is the result: https://gist.github.com/heejune/afd77aeec49e836fa549fc025962ddd8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import asyncio | |
import telepot | |
from telepot.aio.delegate import per_chat_id, create_open | |
import picam | |
""" | |
$ python3.5 countera.py <token> | |
Counts number of messages a user has sent. Starts over if silent for 10 seconds. | |
Illustrates the basic usage of `DelegateBot` and `ChatHandler`. | |
""" | |
class MessageCounter(telepot.aio.helper.ChatHandler): | |
def __init__(self, seed_tuple, timeout): | |
super(MessageCounter, self).__init__(seed_tuple, timeout) | |
async def on_chat_message(self, msg): | |
content_type, chat_type, chat_id = telepot.glance(msg) | |
await bot.sendChatAction(chat_id, 'upload_photo') | |
temp_pic = picam.make_temp_image() | |
result = await bot.sendPhoto(chat_id, open(temp_pic, 'rb')) | |
#file_id = result['photo'][0]['file_id'] | |
#await self.sender.sendMessage(self._count) | |
picam.delete_file(temp_pic) | |
TOKEN = sys.argv[1] # get token from command-line | |
bot = telepot.aio.DelegatorBot(TOKEN, [ | |
(per_chat_id(), create_open(MessageCounter, timeout=10)), | |
]) | |
loop = asyncio.get_event_loop() | |
loop.create_task(bot.message_loop()) | |
print('Listening …') | |
loop.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import picamera | |
import datetime | |
import os | |
def make_temp_image(): | |
cur_time = datetime.datetime.now() | |
with picamera.PiCamera() as camera: | |
camera.resolution = (800, 600) | |
camera.rotation = 180 | |
camera.start_preview() | |
# Camera warm-up time | |
time.sleep(2) | |
photo_path = 'data/{}.jpg'.format(str(cur_time)) | |
camera.capture(photo_path) | |
return photo_path | |
def delete_file(path): | |
os.remove(path) |
why there are two py files, i tried running picam.py, it dose not give any errors, it just comes back on command prompt.
Also what command you are using to receive pics ?
Hi, of course, you can merge two .py files into one. I just wanted to keep files clean as dividing modules as bot and picamera each.
First I suggest you make sure the picamera successfully generate images using picam.py. If you read the coutera.py source code, you’ll realize that it just read images which stored in the sub-directory and send them.
So, if you succeeded generating images on the sub-directory, then make sure telegram bot code keeps running and responses from bot commands. Thanks.
Good day sir,
May I ask for your guidance. Is it possible to adjust the script that you have shown so that when it is launched. it will automatically send the pictures to user telegram? If it is possible. Can you show me how.
1. You can find out more examples using telepot here: https://github.com/nickoala/telepot/tree/master/examples I strongly suggest understand the telepot library itself before working with it further.
2. Please refer to the document. http://telepot.readthedocs.io/en/latest/reference.html The basic bot sendMessage API provides the method you want, so.. it’ll be something like:
TOKEN = sys.argv[1] # get token from command-line
bot = telepot.Bot(TOKEN)
bot.sendMessage(chat_id, …. )
Thanks
could you give the full code after you mergere the both of file .py
thx