Answer the question
In order to leave comments, you need to log in
How to check text for reply_message?
How can I check if a message contains "text"?
(Sometimes, the user does not send this line and an error occurs)
Script:
text = event.raw['object']['fwd_messages'][0]['text']
Answer the question
In order to leave comments, you need to log in
# Проверяет в keys(). Вызовет ошибку, если event.raw['object']['fwd_messages'][0] - не словарь
if 'text' in event.raw['object']['fwd_messages'][0].keys():
pass
# Аналогичная проверка, но, допустим, если event.raw['object']['fwd_messages'][0] - это строка или iterable, не вызовет ошибки, а попытается найти 'text'
if 'text' in event.raw['object']['fwd_messages'][0]:
pass
# Пытается получить из event.raw['object']['fwd_messages'][0] value по ключу "text". Если такого ключа нет в словаре, вернёт default (None)
text = event.raw['object']['fwd_messages'][0].get("text", None)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question