Why is there an "invalid syntax" error when I want my bot to send a photo?

  python, raspberry-pi, telegram-bot

I created a telegram bot to make my Raspberry Pi take photos and videos and sending them to me.

The code which makes the Pi taking the photo runs well, but when I add the line

telegram_bot.sendPhoto(chat_id, photo=open('/home/pi/Pictures/%s.jpg' % name , 'rb')

there’s an "invalid syntax" SyntaxError in this line:

telegram_bot = telepot.Bot('XXX:XXXXXX') .

On top of that, when I add the elif-section there appears another "invalid syntax" SyntaxError in the line at the word "elif".

The whole code looks like that:

#!/usr/bin/python3
#coding: utf8

# telegram import
import time, datetime
import telepot
from telepot.loop import MessageLoop
import os

# camera import
from picamera import PiCamera

camera = PiCamera()


def action(msg):
    chat_id=msg['chat']['id']
    command = msg['text']

    print ('Received: %s' % command)

    if command == '/photo':
        name = datetime.datetime.now()
        camera.capture('/home/pi/Pictures/%s.jpg' % name)
        telegram_bot.sendMessage (chat_id, str("Foto kommt gleich"))
        telegram_bot.sendPhoto(chat_id, photo=open('/home/pi/Pictures/%s.jpg' % name , 'rb')

    elif command == '/video':
        name = datetime.datetime.now()
        telegram_bot.sendMessage (chat_id, str("Video wird aufgenommen"))
        camera.start_recording('/home/pi/Videos/%s.h264' % name)
        sleep(10)
        camera.stop_recording()
        telegram_bot.sendMessage (chat_id, str("Video kommt gleich"))
        telegram_bot.sendVideo(chat_id, video = open('/home/pi/Videos/%s.h264' % name, 'rb')

telegram_bot = telepot.Bot('2132807399:AAEP1ZeVztsoIlvrcTJYQXu7Y9vRHrvmFSU')
print (telegram_bot.getMe())

MessageLoop(telegram_bot, action).run_as_thread()
print ('Up and Running...')

# Keep the program running.
while 1:
    time.sleep(10)

Source: Python Questions

LEAVE A COMMENT