I am preparing a telegram bot with python using telegram-bot. There is the getUpdates() method that theoretically, without additional parameters, returns the last 100 entries that the bot has, whether it is a group, a channel or a private chat between the user and the bot.
With this in mind, I make a call to the mentioned method and it returns the same thing every time, even if I write to the bot in a channel or in private
here the code
bot = telegram.Bot(token=core.telegramToken)
objUpdt = bot.getUpdates()
lista = []
for upd in objUpdt:
for post in [upd]:
print ("tipo = ", type(post['channel_post']['text']))
if (type(post['channel_post']['text']) != 'NoneType'):
print("--",post['channel_post']['text'])
#lista.append(post['channel_post']['text'])
NOTE: The list variable is then used to extract the last record and work with it
Well, this always produces this output:
And as can be seen in this image, the chat has other different entries, since the BEAR - BTCUPUSDT message has been removed in case that strange character in the first image could cause problems and new text has also been added later.
I've been like this all morning and I can't find anything in the Telegram.org document that refers to this problem, I've even looked to see if telegram.org had development forums, but I haven't found anything either, only its documentation
Has this happened to someone? Or do you have any idea where the shots might go?
Well, I answer myself now that I have found the solution.
My code is correct in all its points. My problem arose when I was trying to access "channel_post" from the post variable. It turns out that telegram differentiates between "channel_post" and "message", depending on whether the message was written by a user (administrator or not) (channel_post) or by a robot (message)
In my particular case, I was trying to access messages from a robot but using the index for user messages, so it gave an error every time.
Thanks to @marcelo-graef because although he did not give me the solution itself, it has helped me to better investigate the returned data and in the end that is what has allowed me to detect that difference and reprogram what is necessary.
I did the following tests, and the object "post" is not a dict, but a
therefore it does not allow access to its attributes as a dict.
Using
dir(post)
it I could see that it has a functionto_dict()
, and with that it already allows entering the data.It does not have the field
channel_post
, but it does haveupdate_id
andmessage
(the latter has a lot of data).Here is the part of code I used: