【问题标题】:Is there a way to detect if telegram bot has been started in private?有没有办法检测电报机器人是否已私下启动?
【发布时间】:2021-06-06 14:49:39
【问题描述】:

这个项目是在 pyTelegramBotAPI 模块上开发的。 我需要做的是检查机器人是否已经在私人聊天中启动,因为这主要是在一个群组上运行的。

如果我尝试从群组向尚未启动机器人的玩家发送消息,我的应用会崩溃:

ERROR - TeleBot: "A request to the Telegram API was unsuccessful.
                  Error code: 403. Description: Forbidden: bot was blocked by the user"
@bot.message_handler(commands=['inventory'])
def handle_command_adminwindow(message):
    # add column for each 7 elements in matrix
    rootInventory = ref.child("inventory").get()
    userPool = []
    userId = message.from_user.id
    items = []
    for i in rootInventory:
        userPool.append(i)
    userId = f"{userId}"
    if userId in userPool:
        refItems = ref.child("inventory").child(userId).get()
        for i in refItems:
            items.append(i)
        nitem = len(items)
        while(nitem%7!=0):
            nitem += 1
        #load query witj new data
        inventory(message, items)

然后我宣布我的电话:

def inventario(message, items):
    # List object downloaded from firebase and ready for call actions
    markup = InlineKeyboardMarkup()
    listFruits = ["apple", "banana", "strawberry", "berries", "kiwi", "peach"]
    idGamer = message.from_user.id
    idMsg = message.message_id
    i = 0
    while i < len(items):
        row = []
        if items[i] in listFruits:
            row.append(InlineKeyboardButton(text=items[i], callback_data=f"{idGamer}-cb_f{items[i]}"))
        else:
            row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_other"))
        i += 1
        if items[i] in listFruits:
            row.append(InlineKeyboardButton(text=items[i], callback_data=f"{idGamer}-cb_f{items[i]}"))
        else:
            row.append(InlineKeyboardButton(text=items[i], callback_data=f"cb_other"))
        i += 1
        markup.row(*row)

    bot.send_message(message.chat.id, "You can now check your inventory in pvt\nlink to pvp chat with bot")
    bot.send_message(message.from_user.id, 'Inventory', reply_markup=markup)

【问题讨论】:

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


    【解决方案1】:

    有聊天type属性来区分私聊群聊

    if message.chat.type == "private"
        # private chat message
    else:
        # non-private chat message
    

    其他支持的类型有“channel”、“group”、“supergroup”

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 2023-04-04
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      相关资源
      最近更新 更多