Running a CCTV Telegram Bot on your Pi


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 except using python source

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:

4) I used the 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:

import sys
import asyncio
import telepot
from telepot.aio.delegate import per_chat_id, create_open
import picam
$ python3.5 <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)
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()
print('Listening …')

view raw

hosted with ❤ by GitHub

import time
import picamera
import datetime
import os
def make_temp_image():
cur_time =
with picamera.PiCamera() as camera:
camera.resolution = (800, 600)
camera.rotation = 180
# Camera warm-up time
photo_path = 'data/{}.jpg'.format(str(cur_time))
return photo_path
def delete_file(path):

view raw

hosted with ❤ by GitHub

5 thoughts on “Running a CCTV Telegram Bot on your Pi

  1. why there are two py files, i tried running, it dose not give any errors, it just comes back on command prompt.
    Also what command you are using to receive pics ?

    1. 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 If you read the 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.

  2. 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. 1. You can find out more examples using telepot here: I strongly suggest understand the telepot library itself before working with it further.

      2. Please refer to the document. 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, …. )


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s