Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot retrieve the actual message text on default "*" listener #9

Closed
wzaatar opened this issue May 18, 2020 · 28 comments · Fixed by #10 or #13
Closed

Cannot retrieve the actual message text on default "*" listener #9

wzaatar opened this issue May 18, 2020 · 28 comments · Fixed by #10 or #13
Labels

Comments

@wzaatar
Copy link

wzaatar commented May 18, 2020

@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?

@Paul-weqe
Copy link
Owner

Paul-weqe commented May 18, 2020

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

@Paul-weqe
Copy link
Owner

The sender ID and the message text can be derived from bot.get_messages(room_id=room_id).json()["items"][0]

@wzaatar
Copy link
Author

wzaatar commented May 18, 2020

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.

@Paul-weqe
Copy link
Owner

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.

@Paul-weqe
Copy link
Owner

@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.

@Paul-weqe
Copy link
Owner

Feel free to reopen if you may have any particular issues with how this functionality is working.

@wzaatar
Copy link
Author

wzaatar commented May 25, 2020

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

@wzaatar
Copy link
Author

wzaatar commented May 25, 2020

@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.

@Paul-weqe
Copy link
Owner

Sorry about that I thought it was working just fine when I tested.

I am getting back to you in a couple of minutes.

@Paul-weqe Paul-weqe reopened this May 25, 2020
@Paul-weqe
Copy link
Owner

Paul-weqe commented May 25, 2020

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.

@wzaatar
Copy link
Author

wzaatar commented May 25, 2020

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.

@Paul-weqe
Copy link
Owner

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.

@wzaatar
Copy link
Author

wzaatar commented May 25, 2020

Thanks for your continued efforts and contribution, @Paul-weqe.

@Paul-weqe Paul-weqe linked a pull request May 25, 2020 that will close this issue
@Paul-weqe
Copy link
Owner

THank you very much @wzaatar. Means a lot

@Paul-weqe
Copy link
Owner

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.

@Paul-weqe
Copy link
Owner

The library does not have too many features as it stands right now but these will be added in due time.

@wzaatar
Copy link
Author

wzaatar commented May 26, 2020

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).

@Paul-weqe
Copy link
Owner

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.

@Paul-weqe Paul-weqe reopened this May 26, 2020
@wzaatar
Copy link
Author

wzaatar commented May 26, 2020

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?

@Paul-weqe
Copy link
Owner

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.

@Paul-weqe
Copy link
Owner

Okay, everything is still not okay with the attachment handler. I am still looking into the issue.

Should not be too long with it.

@Paul-weqe
Copy link
Owner

Can you try uninstalling and reinstalling the library now and let me know of any problems @wzaatar

@wzaatar
Copy link
Author

wzaatar commented May 26, 2020

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.

@Paul-weqe
Copy link
Owner

Paul-weqe commented May 26, 2020

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.

@wzaatar
Copy link
Author

wzaatar commented May 26, 2020

Yes, I’ve implemented both. The second one is also throwing an error after your latest update.

@Paul-weqe
Copy link
Owner

Let me have another look and get back.

@Paul-weqe
Copy link
Owner

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

@Paul-weqe
Copy link
Owner

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants