C
C
CatRoys2020-06-23 01:17:13
Python
CatRoys, 2020-06-23 01:17:13

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']

UPD:
Forwarded message like this:
5ef15299960c9455012704.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
werevolff, 2020-06-23
@CatRoys

# Проверяет в 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 question

Ask a Question

731 491 924 answers to any question