I'm writing a little python script using the Telethon library. One of the functions that I'm writing return the word usage frequency of a specific user. The word usage frequency is contained in a String object that is returned to the user in Telegram.
The code for this function is shown below.
@bot.on(events.NewMessage(pattern='/wordsUsage'))
async def start(event):
"""Returns the word usage frequency of a specific user."""
messagesHistory = await client.get_messages(chat_id, None, from_user=event.message.from_id.user_id)
messagesHistory = [i.message for i in messagesHistory if type(i.message) is str]
# I know this line is ugly, let me alone c:
listWords = ' '.join(messagesHistory).replace('\n', ' ').split(' ')
countWord = dict()
for word in listWords:
if word not in countWord.keys():
countWord[word] = 1
else:
countWord[word] += 1
countWord = sorted(countWord.items(), key=lambda item: item[1])
await event.respond(pprint.pformat(countWord, indent=4))
raise events.StopPropagation
When this function is called I get the following error :
telethon.errors.rpcerrorlist.MessageEmptyError: Empty or invalid UTF-8 message was sent (caused by SendMessageRequest)
I don't understand where I'm wrong since Python3 Strings are UTF-8 Strings.