Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """ A simple Telegram bot to get photos from a camera """
- import cv2
- import telepot
- import time
- TOKEN = "****************************"
- PHOTO = 'test.png'
- CAM_PORT = 0
- ramp_frames = 30
- bot = telepot.Bot(TOKEN)
- def help_cmd(bot, update):
- text = ('/start command activates a motion capture mode.\n'
- '/stop command does the opposite.\n'
- '/shot is used to get an actual snapshot.')
- bot.sendMessage(chat_id=update.message.chat_id, text=text)
- def take_photo():
- camera = cv2.VideoCapture(CAM_PORT)
- # camera.set(CV_CAP_PROP_EXPOSURE, 0.0)
- # camera.set(15, -8)
- # camera.set(16, 1)
- # camera.set(12, 50)
- # Wait some time to get ligth in the camera
- time.sleep(3)
- rc, image = camera.read()
- if rc:
- cv2.imwrite(PHOTO, image)
- del(camera)
- return rc
- def send_photo(chat_id, photo_path, caption):
- with open(photo_path, 'rb') as photo:
- bot.sendPhoto(chat_id, photo, caption)
- def handle_messages(msg):
- """ The entry point to the message reception """
- content_type, chat_type, chat_id = telepot.glance(msg)
- print(content_type, chat_type, chat_id)
- if content_type == 'text':
- text = msg['text']
- if text == '/getphoto':
- if take_photo():
- send_photo(chat_id, PHOTO, 'This is a test caption')
- else:
- bot.SendMessage(chat_id, 'A problem occurred taking the photo')
- else:
- #error_msg = "No se de que me hablas!"
- error_msg = "I don't know what are you talking about!"
- bot.sendMessage(chat_id, error_msg)
- bot.message_loop(handle_messages)
- print('Listen messages...')
- while True:
- time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment