Guest User

Untitled

a guest
Jun 27th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """ A simple Telegram bot to get photos from a camera """
  4.  
  5. import cv2
  6. import telepot
  7. import time
  8.  
  9. TOKEN = "****************************"
  10. PHOTO = 'test.png'
  11. CAM_PORT = 0
  12. ramp_frames = 30
  13.  
  14. bot = telepot.Bot(TOKEN)
  15.  
  16. def help_cmd(bot, update):
  17.     text = ('/start command activates a motion capture mode.\n'
  18.             '/stop  command does the opposite.\n'
  19.             '/shot is used to get an actual snapshot.')
  20.     bot.sendMessage(chat_id=update.message.chat_id, text=text)
  21.  
  22.  
  23. def take_photo():
  24.     camera = cv2.VideoCapture(CAM_PORT)
  25. #    camera.set(CV_CAP_PROP_EXPOSURE, 0.0)
  26. #    camera.set(15, -8)
  27. #    camera.set(16, 1)
  28. #    camera.set(12, 50)
  29.  
  30.     # Wait some time to get ligth in the camera
  31.     time.sleep(3)
  32.     rc, image = camera.read()
  33.     if rc:
  34.         cv2.imwrite(PHOTO, image)
  35.     del(camera)
  36.  
  37.     return rc
  38.  
  39. def send_photo(chat_id, photo_path, caption):
  40.     with open(photo_path, 'rb') as photo:
  41.         bot.sendPhoto(chat_id, photo, caption)
  42.  
  43. def handle_messages(msg):
  44.     """ The entry point to the message reception """
  45.     content_type, chat_type, chat_id = telepot.glance(msg)
  46.     print(content_type, chat_type, chat_id)
  47.  if content_type == 'text':
  48.         text = msg['text']
  49.         if text == '/getphoto':
  50.             if take_photo():
  51.                 send_photo(chat_id, PHOTO, 'This is a test caption')
  52.             else:
  53.                 bot.SendMessage(chat_id, 'A problem occurred taking the photo')
  54.         else:
  55.             #error_msg = "No se de que me hablas!"
  56.             error_msg = "I don't know what are you talking about!"
  57.             bot.sendMessage(chat_id, error_msg)
  58.  
  59. bot.message_loop(handle_messages)
  60. print('Listen messages...')
  61.  
  62. while True:
  63.     time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment