有没有办法检测电报机器人是否已私下启动?

问题描述

这个项目是在 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"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)

解决方法

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

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

其他支持的类型是“频道”、“组”、“超级组”

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...