Cannot retrieve the actual message text on default "*" listener
Closed this issue · 28 comments
@Paul-weqe, I don't seem to find a way to retrieve the message text or Sender ID programmatically if using @bot.on_hears("*")
. Is this by design?
Hi @wzaatar
By default, there is no way of getting all the details of a message directly from the
@bot.on_hears("*")
decorator.
You can get details of the latest message sent in the room by the following:
@bot.on_hears("*")
def hear_everything(room_id=None):
print(bot.get_messages(room_id=room_id).json()["items"][0])
# continue with logic
bot.get_messages(room_id=room_id).json()["items"][0]
is supposed to get you the latest message sent in the room which is assumed to be the message that trigerred the decorator.
Really unfortunate how such an important detail was missed during the creation of the library. A branch will be created for this and you will be updated once the PR for the issue has been completed.
I hope the workaround provided above can prove useful
The sender ID and the message text can be derived from bot.get_messages(room_id=room_id).json()["items"][0]
Yes, I could figure that. Just wasn’t sure if I missed a more straightforward way of doing it. May I suggest you include a way to retrieve the actual message? That’ll allow more flexibility in coding a response.
Thanks for your hard work.
That is definitely next on things to do for the library, I'll make time and work on it. Again I don't know how I could have missed such an important element, because users will constantly want to see this for every request.
And thank you, I will leave this issue open for now and close it once I have the PR on this issue completed. Your comments are much appreciated.
Thank you a lot.
@wzaatar You should now be able to print out the message details using the following:
@bot.on_hears("hi")
def hear_everything(room_id=None, message_info=None):
print(message_info)
# continue with logic
This will give us a dictionary with the information including the message text.
NOTE There should be no consequence of leaving out the message_info
as part of the arguments but it is compulsory for it to be present if you are to collect information from the message.
Happy coding and feel free to contribute.
Feel free to reopen if you may have any particular issues with how this functionality is working.
Getting the following error...
File "/usr/local/lib/python3.8/site-packages/python_webex/webhook/__init__.py", line 61, in index if 'message_info' in bot.hears_to_function[message_text].__code__.co_varnames: UnboundLocalError: local variable 'message_text' referenced before assignment
@Paul-weqe, library is now unusable. Every call to bot.hears_to_function
is now throwing this error. I'm reverting to the prior version.
Sorry about that I thought it was working just fine when I tested.
I am getting back to you in a couple of minutes.
I have had a look at this and it specifically throws an error when you are looking for a general response e.g
@bot.on_hears("*")
but on the rest specific message responses e.g
@bot.on_hears("hi")
There is no error being thrown. I am working on fixing what the issue is with the first case.
If your error is being thrown on the other scenarios except the python @bot.on_hears('*')
you may need to elaborate further what the issue might be.
You are correct. It’s on the catch-all call that the error is showing up... But that’s where it’s mostly needed, I think.
You are absolutely right. Should be fixed in one hour tops. Sorry for the inconvenience .
Will update you as soon as the issue is fixed.
Thanks for your continued efforts and contribution, @Paul-weqe.
Can you try reinstalling the library and try the same thing again:
@bot.on_hears("*")
def hears_all(room_id=None, message_info=None):
print(message_info)
return bot.send_message(room_id=room_id, text="Hello, come again")
Or the logic you want to apply. Let me know if it still throws an error or works just fine now on your end.
If not, it should be fine to close this issue and merge the PR.
The library does not have too many features as it stands right now but these will be added in due time.
Hi, @Paul-weqe. All works except for the default attachment handler, which is now returning the same error message you fixed earlier for the default message handler. This happens only if an attachment is sent without any text (which is probably raising an exception in your code).
I should probably stop closing this issue too early lol. Sorry about that, I am still learning to adapt maintaining an externally used library.
I am having a look. It will probably be fixed in the hour. You can try out other things I and let me know what breaks. I will need that kind of information to improve the library. Thank you.
I'm also trying a
@bot.set_file_action("*") def custom_response(files, room_id=None, message_info=None)
to intercept all attachments with any text. Would that be the right way to do it in your implementation?
I have just uploaded a new version you can try and reinstall and see. I am currently testing everything you have mentioned above again and see if there is any problem.
For the question above, yes that should work:
@bot.set_file_action("*")
def default_response(files, room_id=None, message_info=None):
# do something
I will let you know once everything is ready for use. Should be a couple of minutes.
Okay, everything is still not okay with the attachment handler. I am still looking into the issue.
Should not be too long with it.
Can you try uninstalling and reinstalling the library now and let me know of any problems @wzaatar
Great work, @Paul-weqe. It’s all working fine except one scenario: Receiving an attachment without any text message, your default file handler basically. The message_info variable returns an error.
Sorry for late response on this one.
For handling any type of text being sent that hasn't been handled with a response, we use:
@bot.set_file_action("*")
def default_file_text(files, room_id=None, message_info=None):
print(message_info)
For handling the files without text, we set:
@bot.set_default_file_response()
def file_response_without_text(room_id, files=None, message_info=None):
print(message_info)
bot.send_message(room_id=room_id, text="This is the default response for files without a text")
I hope this helps.
Yes, I’ve implemented both. The second one is also throwing an error after your latest update.
Let me have another look and get back.
I will need more context on this because everything is working just fine on my end with this one.
Do you have a snippet I can have a look at
Here is what I am using on my end and it is able to fetch the data and give a valid response
@bot.set_default_file_response()
def def_response(room_id, files=None, message_info=None):
print(message_info)
return bot.send_message(room_id=room_id, text="Try it again?")
@bot.set_file_action("*")
def file_action(files, room_id=None, message_info=None):
print(files)
print(message_info)
return bot.send_message(room_id=room_id, text="Yeah, received that. Thank you")