当新用户加入该机器人所在的组时,我正在使用Telegram机器人的Webhook接收Update对象。我希望收到有关该用户的更多信息,但我得到的只是:< / p>
"new_chat_members": [
{
"id": xxxxxxxxx,
"is_bot": false,
"first_name": "xxxxx"
}
]
我知道我可以使用getFullUser API端点,但是我不想提出其他要求。有没有办法将用户名包含到那里接收的数据中?
答案 0 :(得分:0)
如果您执行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))