Sending message to different channel via slack webhook fails - webhooks

I'm not sure if I am understanding the way how to use webhooks quite right, but:
I want to send messages in different channels or to different users (not at once), without being involved in that conversation.
My problem: I can only create webhooks for specific users, so I end up having a unique URL for every user / channel?
If I use something like the "channel" parameter or even the "setReceipent" method from the library I use, it doensn't have any effect and only the channel / user the webhook was created for, receives the message.
Do I need to use API access or can I accomplish my needs using webhooks?

In general incoming webhooks are fixed to the configured channel. So if you want to send messages to users and/or multiple channels you need to create multiple webhooks or send messages though the API (e.g. chat.PostMessage).
However, there is a another way to create webhooks, that allows you to send messages to every channel with the same webhook by adding a channel override property ('channel') to your message. Its how incoming webhooks used to work in the past and part of legacy custom integrations.
To create such a webhook you need to install an app called "Incoming webhooks" from the Slack App Directory (app is made by the Slack team).
Syntax:
POST https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Content-type: application/json
{
"text": "Hello, world.",
"channel": "U12345678"
}
Where U12345678 is the Slack ID of the user you want to send a direct message to.
Note that the channel property is optional and the message will be send to the default channel if omitted.
See here fore the full documentation.

Hooks only allow you to send to the channel defined in the hook.
If you want to send to any channel you need to create a bot user who can post to any channel. To create a bot user you need to do the following:
Add an API app Once done create a bot user (or create and delete a webhook )
Create a bot user or create and delete a webhook (which will create the bot user for you)
Add chat:write and possibly chat:write.public to the OAuth & Permissions of the API App.
Grab the Bot User OAuth Access Token it should start xoxb
Post to https://slack.com/api/chat.postMessage
eg
curl -X POST \
-H 'Authorization: Bearer xoxb-###-###-***' \
-H 'Content-type: application/json' \
--data '{"channel": "#general","text":"Hello, World!"}' \
https://slack.com/api/chat.postMessage
https://api.slack.com/messaging/sending#publishing provides some details

Related

Node and Microsoft Teams API

I'm building a Node only application that reads logs in the background and based on an event being read will send a message to a Teams channel directly.
I've been having quite a few issues getting a Graph API access token valid through Username and password.
I have been able to get a Graph API access token with client secret and tenant id which represents access
"without a user". Now that does not allow me to post a message in a channel as I would need to have access "on behalf of a user".
API => https://graph.microsoft.com/v1.0/teams/{team-id}/channels/{channel-id}/messages .
Would there be another way of achieving this? Webhook/Connectors?
Thank you!
There's a few different ways you can post to a teams channel, you can set up like you said an http webhook, where you could call it to post into a channel https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using
You can use power automate (flow) or logic apps to post messages as the flowbot, or you can write a bot/ use the bot framework to register a bot that can post to teams, called proactive messaging: https://learn.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bot-conversations/bots-conv-proactive
As for trying to use graph with application permissions, that's not possible, at least for the moment.

Slack bot posting in a conversation without being in it

Everything I see around slack bot say that a bot cannot post within a conversation it's not been invited to.
➡️ However, why /giphy or simple poll (/poll) can post in PM (one to one) OR in a conversation where the bot has not been added to?
The current permissions are chat:write,commands,files:write
The oauth token is a "Bot User OAuth Access Token"
The plan is to support the Add to slack and to distribute the app.
Functionality wise, it support a /command and post file to a conversation.
The bot can:
- receive the oauth token uppon app install (via add to slack)
- delete ephemeral messages
- post a file only where it's been added too, which suck because the bot does not need to read any message within anything.
Code of the bot here
If the bot is not invited too, the answer is either channel_not_found or not_in_channel.
What am I missing?
It is correct that bots in general can only post messages to channels they are invited to. However, there are two exceptions:
Bots can inherit the rights of the installing user. So bots can post to every private channel the installing user is a member of (including direct message channels).
Slash commands work differently. If a user issues a slash command from a channel the bot can reply to that slash command. That works for every channel and does not require the bot to be invited. Those replies are normal Slack messages, so the can contain all kind of message features incl. image attachments or blocks with images.
A common pattern to give bot full access to private channels is the following:
User starts the bot with a slash command
Bot checks if it has full access to the current channel
If not it asks the user to invite it to that channel

Slack incoming webhook vs postMessage Web API

What is the difference between incoming webhook and slack chat.postMessage webAPI?
When should we use one over another? I have a slack app, I want to send messages to particular channel as a User. I found an option to post message as a User in chat.postMessage, can I achieve this by incoming webhook?
To your specific question. Its technically possible to send messages on behalf of a user with chat.postMessage by using a token from that user and setting as_user to true.
For example if you installed a Slack app the created app token will be linked to the installing user and you can use it send messages on behalf of the installing user.

Not receiving Square Connect Webhook for INVENTORY_UPDATED events

I have followed the instructions for setting up Square Connect web hooks.
I entered my Notification URL in the developer portal and enabled web hooks. I then sent a test notification. I verified that is working correctly.
I subscribed to "INVENTORY_UPDATED" with the Update Webhooks endpoint.
I verified that worked by calling /v1/{{location_id}}/webhooks. The response is
["INVENTORY_UPDATED"]
So I know that my location is properly subscribed to INVENTORY_UPDATED notifications.
But when I sign into the dashboard and create new items or update the inventory
count on those items, I don't receive any notifications.
I'm stumped.
Do inventory updates in the dashboard trigger INVENTORY_UPDATED webhook events? If so, did I miss a step?
Any suggestions of how to troubleshoot this?
You need to subscribe to the webhook notification by:
open your terminal or cmd (on your PC).
Run (curl -X PUT -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -H "Content-Type: application/json" -d "[\"INVENTORY_UPDATED\"]" https://connect.squareup.com/v1/YOUR_LOCATION_ID/webhooks)
NOTE: Your Access token and Location are found on the dashboard of your square account.

slack incoming webhook - send message to another channel

I am trying to post message to any channel in a slack team.
My webhook has been properly created and installed into my test team. As part of the install process, I selected "Post to #channel1"
When I review permissions, I see my app has been granted Other/Post to specific channels in Slack (#channel1).
From there, I am able to post into #channel1 with a basic JSON request:
{
"text":"Hello, World!"
}
Now I want to validate I can use same webhook to post to any other channel of the same team. So I created a channel #channel2 and I try the following request:
{
"channel":"#channel2",
"text":"Hello, World!"
}
The request is accepted (HTTP Status 200) but it still publishes on #channel1.
What am I doing wrong ? Is that a syntax or permission issue ? I guess it is more a permission issue but I have not found how to grant my app access to additional channels (or all channels).
Regards.
I created an incoming Webhook, and I found this documentation (see image)
I followed the instructions given in that, and I found that it worked. I can send a message to a different channel than the one the Incoming Webhook was set for
The app needs the chat:write:bot permission to post messages to other channels.
The permission can be added in Manage Apps → your app → OAuth & Permissions:
Reading Slack's own documentation, this was not obvious to me. (although it is mentioned under "Using the Web API" in the Intro to Messaging.)

Resources