mrkaye97 / slackr

An R package for sending messages from R to Slack
https://matthewrkaye.com/slackr/
Other
307 stars 83 forks source link

how to send a direct message to users? #173

Closed hkboo closed 2 years ago

hkboo commented 2 years ago

Hello, i wonder slackr supports "A bot send user dm, not to channel?"

mrkaye97 commented 2 years ago

You can use channel = "@foo.bar" to send a message to a specific user. To get a list of usernames, use slackr_users().

Who the message will be sent from depends on the type of token you have set up. If you're using a bot token (xoxb-...), it'll send from the Slackbot. If you're using a user token (xoxp-...) it'll send from your username.

hkboo commented 2 years ago

@mrkaye97 thank you for your answering. Now i knew using 'name' col ... not 'real_name' col ( that column' naming from slackr_users() )

When sending dm, Is there a way to use 'real_name' col instead of 'name' col?

mrkaye97 commented 2 years ago

No. You need to use name or id because that's what the API expects. If you want to use real_name, you can do something like this:

library(dplyr)

get_name <- function(real_name) {
    slackr_users() %>%
        filter(real_name == !!real_name) %>%
        pull(name) %>%
        sprintf(
            "@%s", .
        )
}

slackr(
    "Testing",
    channel = get_name("Foo Bar")
)

Created on 2021-12-14 by the reprex package (v2.0.1)

hkboo commented 2 years ago

How to use 'real_name' col? Check my codes T.T

1. Returned user list

library(dplyr)

slackr_users() %>% 
    filter(is_bot == FALSE) %>% 
    select(id, name, real_name)
id name real_name    
USLACKBOT slackbot Slackbot    
U02QG6VJ678 hkboo Boo

2. Run slackr_msg()

mrkaye97 commented 2 years ago

You can use the code I pasted before:

library(dplyr)

get_name <- function(real_name) {
    slackr_users() %>%
        filter(real_name == !!real_name) %>%
        pull(name) %>%
        sprintf(
            "@%s", .
        )
}

slackr(
    "Testing",
    channel = get_name("Foo Bar")
)

Then get_name("Boo") will return the <<name>>, which you can then use as the channel.

hkboo commented 2 years ago

Oh, I misunderstood as 'name' column is able to be entered directly into the parameter. Now I understand. Thank you!