【问题标题】:Telegram bot webhook doesn't receive new chat member usernameTelegram bot webhook 没有收到新的聊天成员用户名
【发布时间】:2019-01-12 08:08:00
【问题描述】:

当新用户加入机器人所在的组时,我正在使用 Telegram 机器人 webhook 接收 Update 对象。我期待收到有关该用户的更多信息,但我得到的只是:

"new_chat_members": [
    {
        "id": xxxxxxxxx,
        "is_bot": false,
        "first_name": "xxxxx"
    }
]

我知道我可以使用getFullUser API 端点,但我不想提出额外的请求。有没有办法将用户名包含到那里收到的数据中?

【问题讨论】:

  • 我也有同样的问题,你解决了吗?

标签: telegram telegram-bot telegram-webhook


【解决方案1】:

如果你bot.send_message(cid, str(m))你会得到消息的所有信息:

{
    'left_chat_member': None,
    'migrate_from_chat_id': None,
    'content_type': 'new_chat_members',
    'date': '1533552871',
    'voice': None,
    'migrate_to_chat_id': None,
    'group_chat_created': None,
    'location': None,
    'sticker': None,
    'new_chat_photo': None,
    'forward_from': None,
    'channel_chat_created': None,
    'video': None,
    'pinned_message': None,
    'supergroup_chat_created': None,
    'chat': {
        'id': idGroup,
        'title': 'groupTitle',
        'first_name': None,
        'last_name': None,
        'username': None,
        'all_members_are_administrators': None,
        'type': 'supergroup'
    },
    'audio': None,
    'reply_to_message': None,
    'caption': None,
    'contact': None,
    'document': None,
    'entities': None,
    'message_id': '688',
    'photo': None,
    'new_chat_title': None,
    'successful_payment': None,
    'forward_from_chat': None,
    'venue': None,
    'forward_date': None,
    'invoice': None,
    'text': None,
    'edit_date': None,
    'video_note': None,
    'from_user': {
        'username': 'username',
        'last_name': None,
        'first_name': 'userFirstName',
        'id': id,
        'language_code': 'es'
    },
    'delete_chat_photo': None,
    'new_chat_members': [{
        'id': id,
        'is_bot': True,
        'first_name': 'UserName',
        'username': 'newUserName'
    }],
    'new_chat_member': {
        'username': 'username',
        'last_name': None,
        'first_name': 'firstNameUser',
        'id': idUser,
        'language_code': None
    }
}

我的代码在python3。您可以试试这个并添加有关新用户的更多信息:

@bot.message_handler(func=lambda message: True, content_types=['new_chat_members'])
    def command_hi(m):
        cid = m.chat.id
        cname = m.chat.title
        idUser = message.from_user.id

        welcome = ""
        if (m.new_chat_member.username is None):
            nun = m.new_chat_member.first_name
            if (m.new_chat_member.last_name is not None):
                nun += " "
                nun += m.new_chat_member.last_name
            else:
                welcome= "Welcome to the group"
                welcome += str(cname)
                welcome += " "
        else:
            nun = m.new_chat_member.username
            welcome= "Welcome to the group  "
            welcome+= str(cname)
            welcome+= " @"

        bot.send_message(cid, str(welcome) + str(nun))

【讨论】:

    猜你喜欢
    • 2016-12-05
    • 2022-01-24
    • 2020-04-14
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2018-03-12
    • 2016-05-17
    相关资源
    最近更新 更多