pyTelegramBotAPI
pyTelegramBotAPI copied to clipboard
Handle new user in chat
Please answer these questions before submitting your issue. Thanks!
-
3.6.3
-
ubuntu 18.04(x64)
-
3.5
I have a question. How can I handle event with pytelegrambotapi when new user appears in the chat?
Thanks in advance.
If somebody is interested in this issue, problem was solved in 1 hours with strange method.
url = 'https://api.telegram.org/bot{0}/getUpdates'.format(bot_token)
result = requests.post(url)
try:
offset = json.loads(result.text)['result'][0]['update_id']
except IndexError:
offset = -1
while True:
url = 'https://api.telegram.org/bot{0}/getUpdates'.format(bot_token)
result = requests.post(url, params={'offset': offset})
json_string = json.loads(result.text)['result']
for update in json_string:
if 'new_chat_members' in update['message']:
members = update['message']['new_chat_members']
for member in members:
bot.send_message(update['message']['chat']['id'], service.get_text(14).format(member['first_name'], bot_name))
data = telebot.types.Update.de_json(json.dumps(update))
bot.process_new_updates([data])
offset = int(json.loads(result.text)['result'][0]['update_id']) + 1
sleep(0.5)
You can check message's content_type is 'new_chat_members' or not.
content_type