Python远程机器人脚本(每分钟拍一张照片并发送至电报)

问题描述 投票:0回答:1

嗨,我一直在与这个脚本斗争了一段时间,最后一行开始让我疯狂。我想每分钟拍一张照片并把它发送到telegram。

import telebot
import picamera

# Set token
token = "PLACE TOKEN HERE"

# Connect to our bot
bot = telebot.TeleBot(token)

# Sets the id for the active chat
chat_id=PLACE CHAT ID HERE

# Get the photo
camera=picamera.PiCamera()
camera.capture('./capture.jpg')
camera.close()

# Sends a message to the chat
bot.send_photo(chat_id, photo=open('./capture.jpg', 'rb'))

ERROR: TypeError: send_photo()得到了一个意外的关键字 "photo"。

帮助将是非常感激的。

python python-telegram-bot
1个回答
0
投票

我已经改进了这个脚本。如果对大家有帮助,这里是我目前使用的代码。

import time
import requests
import telebot
import picamera
from time import sleep

# Set token
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Connect to our bot
bot = telebot.TeleBot(token)

# Sets the id for the active chat
chat_id="xxxxxxxxx"

# Repeat function
def repeat():
    # Set camera
    camera=picamera.PiCamera()
    # Take picture
    camera.capture('./capture.jpg')
    # Camera close
    camera.close()
    # Send photo to telegram
    bot.send_photo(chat_id=chat_id, photo=open('capture.jpg', 'rb'))
    # Sleep 60 seconds
    time.sleep(60)
while True:
  repeat()

它可以工作,但有时脚本会因为这些错误而崩溃:

requests.exceptions.ConnectionError:('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

requests.exceptions.ConnectionError:('连接中止。', timeout())

知道如何解决这些错误吗?

© www.soinside.com 2019 - 2024. All rights reserved.