【问题标题】:How to get photos using pyTelegramBotAPI?如何使用 pyTelegramBotAPI 获取照片?
【发布时间】:2021-07-13 23:54:16
【问题描述】:

我正在尝试从 Telegram 机器人实现这个逻辑(我使用客户端的桌面版本):

  1. 机器人要求发送照片
  2. 用户发送照片(分组和不分组)
  3. 机器人接收用户发送的所有照片
  4. 只有在收到所有照片后,bot 才会发送一条操作成功的消息,并将控制权移交给下一个函数。

我的代码是:

import telebot

bot_token = '1279****77:AAHJDPFJ************KJfLbiI'
bot = telebot.TeleBot(bot_token, parse_mode='html')
photo_list = []

@bot.message_handler(commands=['start'])
def send_welcome(message):
    send = bot.send_message(message.from_user.id, 'Send your pics...')
    bot.register_next_step_handler(send, get_user_pics)
    return

@bot.message_handler(content_types=['photo'])
def get_user_pics(message):
    if message.photo[-1].file_id not in photo_list:
        photo_list.append(message.photo[-1].file_id)
        if len(photo_list) == 1:
            send = bot.send_message(message.from_user.id, "Photos received...")
            bot.register_next_step_handler(send, process_messages())
            return

def process_messages():
    print(photo_list)
    return

bot.polling()

现在,该逻辑仅在用户发送带有分组的照片时才有效。 如果发送时没有分组(约 10 张照片),那么在大约第 4 张照片之后,机器人会写道它成功拍摄了照片,没有其他任何东西...... 那么无论照片是否分组,我如何实现所需的逻辑?

【问题讨论】:

    标签: python telegram-bot py-telegram-bot-api


    【解决方案1】:

    尝试做这样的事情:

    import telebot
    
    bot_token = '1279****77:AAHJDPFJ************KJfLbiI'
    bot = telebot.TeleBot(bot_token, parse_mode='html')
    photo_list = []
    
    @bot.message_handler(commands=['start'])
    def send_welcome(message):
        send = bot.send_message(message.from_user.id, 'Send your pics...')
        bot.register_next_step_handler(send, get_user_pics)
        return
    
    @bot.message_handler(content_types=['photo'])
    def get_user_pics(message):
        if message.text == '/done':
            process_messages()
            return
        elif message.photo[-1].file_id not in photo_list:
            photo_list.append(message.photo[-1].file_id)
        send = bot.send_message(message.from_user.id, "Send another photo or click /done")
        bot.register_next_step_handler(send, get_user_pics)
        return
    
    def process_messages():
        print(photo_list)
        return
    
    bot.polling()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多