代码之家  ›  专栏  ›  技术社区  ›  Paul Razvan Berg

Telegram bot webhook没有收到新的聊天成员用户名

  •  0
  • Paul Razvan Berg  · 技术社区  · 8 年前

    我用电报机器人网络钩子接收 Update 对象当新用户加入bot所在的组时。我希望收到更多关于用户的信息,但我得到的只是:

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

    我知道我可以用 getFullUser API端点,但我不想再提出请求。有没有办法把用户名包括在接收到的数据中?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Paul Razvan Berg    8 年前

    如果你这么做的话 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))