Skip to content

Instantly share code, notes, and snippets.

@heejune
Created July 19, 2016 16:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heejune/afd77aeec49e836fa549fc025962ddd8 to your computer and use it in GitHub Desktop.
Save heejune/afd77aeec49e836fa549fc025962ddd8 to your computer and use it in GitHub Desktop.
Telegram bot takes a photo using a pi camera and replies back running on Raspberry Pi
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()
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment