Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Last active May 11, 2024 05:29
Show Gist options
  • Save IdrisCytron/c48016363ac9f62fba1a4db860c482dc to your computer and use it in GitHub Desktop.
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
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