Last active
May 11, 2024 05:29
-
-
Save IdrisCytron/c48016363ac9f62fba1a4db860c482dc to your computer and use it in GitHub Desktop.
Raspberry Pi Camera Telegram Bot. Youtube - https://www.youtube.com/watch?v=zAU0pKrk24o
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 telepot | |
import picamera | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
def handle(msg): | |
global sendPhoto | |
global chat_id | |
chat_id = msg['chat']['id'] | |
command = msg['text'] | |
print('Message received from ' + str(chat_id)) | |
if command == '/photo': | |
sendPhoto = True | |
elif command == '/start': | |
bot.sendMessage(chat_id, 'Welcome to Cytron Photo Bot') | |
else: | |
bot.sendMessage(chat_id, 'Invalid command.') | |
def capture(): | |
print('Capturing photo...') | |
camera = picamera.PiCamera() | |
camera.capture('./photo.jpg') | |
camera.close() | |
print('Sending photo to ' + str(chat_id)) | |
bot.sendPhoto(chat_id, photo = open('./photo.jpg', 'rb')) | |
bot = telepot.Bot('YOUR_TELEGRAM_BOT_TOKEN') | |
bot.message_loop(handle) | |
print('Bot ready!') | |
sendPhoto = False | |
try: | |
while True: | |
if sendPhoto == True: | |
sendPhoto = False | |
capture() | |
except KeyboardInterrupt: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment