caronc / apprise

Apprise - Push Notifications that work with just about every platform!
https://hub.docker.com/r/caronc/apprise
BSD 2-Clause "Simplified" License
11.71k stars 410 forks source link

Discord: notify user directly or tag him on channel #989

Closed michalfratczak closed 7 months ago

michalfratczak commented 11 months ago

:question: Question I can't find this information anywhere. How do I notify user directly or tag him on a channel ? I tried including "@discord-user-name" in the title or body of the message, but this simply is displayed as raw text - user is not "pinged".

I am using apprise in python script:

apobj = apprise.Apprise()
apobj.add('discord://ID/TOKEN/')
apobj.notify(title = '@michal_f', body='message')
caronc commented 11 months ago

I had a look at section of the Discord API that deals with sending pings.

# ping `@everybody`
# ping the user id `123`
# ping the role id `456`
apprise -vvv -b "@everybody, wake and meet our new user <@123>; <@&456>" \
   "discord://credentials"

Since apprise does not create a allowed_mentions directive in the background; there should be no restrictions placed on you.

Also, In your example you placed the @ in the title... what happens if you place it in the body instead?

michalfratczak commented 11 months ago

Thanks for replying. Placing @ in the body does not help. I tried literally every combination of special characters I could come up with - I can't ping a user

caronc commented 10 months ago

I think I fixed this :slightly_smiling_face: . Would you be able to follow my PR (link above this comment) and test it out; the instructions can be found in the link.

Edit : Keep in mind you need to put the @ reference in the body, not the title

michalfratczak commented 10 months ago

Hi, thanks for looking into it so quickly and sorry for late response :)

I installed that branch but using API from a python script does not seem to work. I made sure to put @ in body.

At the same time, I cannot use and test apprise command - it must be some installation quirk. I tried both on windows and on Ubuntu on VM - in both cases apprise command is not found. What could go wrong here? I'd still like to run the test as you did, from command line.

caronc commented 10 months ago

The branch from the Apprise API source points too Apprise (underlining library) v1.6.0. the change made for this took place afterwards so unless you pull the commit into it, it won't appear until the next official release

michalfratczak commented 10 months ago

I installed as instructed in PR link above pip install git+https://github.com/caronc/apprise.git@989-discord-notifications Notifications do not work from API.

caronc commented 10 months ago

Can you share a sample of the payload you're using? I have an Apprise Discord channel and I was able to test it and have it work very well.

Try something like @here. If it isn't tagging then we may need to investigate how you're creating your Docker container. I'm traveling so my responses are via my phone right now.

You can also use the master branch as this specific branch was already merged into it.

michalfratczak commented 10 months ago

Tagging @here works OK. I am not using docker, just installing into OS python. maybe I am reffering username in a wrong way. Discord can display different user names

Touschusss commented 10 months ago

Hi, For me ping works if you use 'text' format and the <@123> or <@&456> syntax depending you mention a User or a Role. But it didn't work until now for 'Markdown' format. Maybe ping is only supported in the 'content' field of the Json?

This addition is great as i was looking for it too in order to avoid 2 notifications (one to ping, and one for text with nice look).

Netherless, from my point of view, one more thing could be nice: Clean up the body of the <@123> and <@&456> to avoid mentioning them twice. The sender should then put one mention of User or Role per line to ensure nice rendering.

I did make the change for myself as it's a really small change but it would be better in master realese.

Thanks for the great work!

caronc commented 10 months ago

For me ping works if you use 'text' format and the <@123> or <@&456> syntax depending you mention a User or a Role. But it didn't work until now for 'Markdown' format. Maybe ping is only supported in the 'content' field of the Json?

You're correct, this is exactly the case. The fix was to parse the markdown and extract the Tags so i could place them into the context field (letting the parsing work)

Netherless, from my point of view, one more thing could be nice: Clean up the body of the <@123> and <@&456> to avoid mentioning them twice.

I'm not sure if this is the case, the mention only goes out once.

The sender should then put one mention of User or Role per line to ensure nice rendering.

I did make the change for myself as it's a really small change but it would be better in master realese.

It sounds like it's a good idea, but I don't quite understand it 🙂. You made a change they cleans up what is there? Can you share with me the change you made? Are you swapping these values for something else? I apologize for not quite picking up on your suggestion/solution based on just your description.

Touschusss commented 10 months ago

Netherless, from my point of view, one more thing could be nice: Clean up the body of the <@123> and <@&456> to avoid mentioning them twice.

I'm not sure if this is the case, the mention only goes out once.

To explain what i mean here is a screenshot of what i got with the last 'NotifyDiscord.py' from the git. 1 7 0

You can see the Role is mentioned twice: One in the ping, one in the body.

My goal was to get this rendering: 1 7 1

So, to not have the mention of the role in the body.

The sender should then put one mention of User or Role per line to ensure nice rendering. I did make the change for myself as it's a really small change but it would be better in master realese.

It sounds like it's a good idea, but I don't quite understand it 🙂. You made a change they cleans up what is there? Can you share with me the change you made? Are you swapping these values for something else? I apologize for not quite picking up on your suggestion/solution based on just your description.

What i did to achieve this is very simple:

if self.notify_format == NotifyFormat.MARKDOWN:
                # Move here the line looking for Role detection
                results = USER_ROLE_DETECTION_RE.findall(body)

                # Cleaning up the Role from body to a new var
                new_body = USER_ROLE_DETECTION_RE.sub('', body)

                # Assigning new content to original body
                body = new_body

So in the end it looks like i wanted it to be. I'm not a python expert so maybe it can be better to do other way.

Hope this clarify all.

caronc commented 9 months ago

Your suggestion would prevent strings like:

hey @everybody! how are you all doing?

Your code would strip that out and it would be:

👉 @everybody
hey ! how are you all doing

I didn't want to obstruct the original message with my integration. but since i had to pull the pinged endpoints out of the message block for them to be actually triggered, you get the duplication that you see.

caronc commented 7 months ago

Closing off issue as complete.