mrkaye97 / slackr

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

Why can't we use webhook for functions other than `slack_bot()`? #171

Closed Bisaloo closed 2 years ago

Bisaloo commented 2 years ago

slack_bot() works fine when provided with just an incoming webhook. Why do the other functions (such as slackr() or slack_msg() require more arguments (token, username, etc.) and do not allow to pass just the webhook?

mrkaye97 commented 2 years ago

@Bisaloo thanks for the question.

Per the slackr_bot docs:

[slackr_bot] Takes an expr, evaluates it and sends the output to a Slack chat destination via the webhook API

slackr, slackr_msg, etc. do something different. Instead of taking a webhook and being attached to a single channel, they leverage a scoped bot or user token to post to channels in the workspace. On the back end, this means that the request getting sent to the Slack API is not just a webhook URL, but instead relies on Slack API methods such as chat.Postmessage to post messages, plots, upload files, etc. Fundamentally, these are different tasks, which is why slackr_bot exists for sending webhook messages and the other slackr* functions exist for using other Slack API methods.

Does that answer your question?

You can learn more about the Slack API here: https://api.slack.com/start and you can read about individual methods here: https://api.slack.com/methods

mrkaye97 commented 2 years ago

Also, FWIW, slackr, slackr_msg, and slackr_bot are all very similar in practice. It's mostly the other slackr* functions that differ significantly. For the most part, you can accomplish almost anything you could with slackr or slackr_msg with slackr_bot, and slackr_bot requires significantly fewer scopes to do so. This means that slackr_bot is generally easier to set up, more secure, etc.

Bisaloo commented 2 years ago

But the issue is that slackr_bot() doesn't allow posting just a comment as far as I can tell. Because it has been designed the idea that people will use it to evaluate R expressions, it will echo this expression alongside the result and format it as a quote.

Another way to formulate my question/request: could there be an easy function (as easy as slackr_bot()), i.e., using just the webhook mechanism, to post a simple comment? Do we need to complexity around slack_msg()? Wouldn't we be better off with an interface similar to slackr_bot()?

mrkaye97 commented 2 years ago

I'm not totally sure what you mean by "just a comment". You mean you want behavior like this?

slackr_bot("foobar")

such that in Slack you just see:

foobar

? slackr_bot() should already allow for this behavior, but let me know what I'm missing

Bisaloo commented 2 years ago

Ah, I think the problem comes from the fact that I don't want to do directly

slackr_bot("Hello world")

but

test <- "Hello world"
slackr_bot(test)

because the string is created programmatically.

In this situation, the output is

> test [1] Hello world

which is not what I want.

mrkaye97 commented 2 years ago

Interesting, okay. I see what you mean. I think this is a bug, but I'm not 100% sure. When a get a minute, I'll look into it and report back.

The confusing thing to me is that the internals for slackr() and slackr_bot() are almost identical, so I'm not sure how you'd get this behavior with slackr_bot() when I can't repro it with slackr().

Regardless, thanks for the bug report, and I'll report back

mrkaye97 commented 2 years ago

Hey @Bisaloo -- just circling back. So I tested this out, and my mental model was a little bit off. slackr and slackr_bot evaluate expressions the same way, which is why you get the behavior that you do. slackr_msg will allow you to send the text as if it were just text (i.e. not an object with a name and a value), but if you're using a bot token, slackr_msg won't work for you. That seems to be what you were describing in your previous comments. One possible solution to this would be to introduce a slackr_bot_msg function that operates like slackr_msg. I could do this, and will consider it.

I'll let you know if I make that change

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.