Slack webhook and direct/private messages - slack-api

I created a websocket-based Slack bot (that plays chess). In order to return a graphical representation of the board (so PNG instead of simple ASCII) I must use a webhook since normal messages cannot have attachments.
The interaction with the bot is through direct messages and I have 1 webhook. If I set the channel in the wehook to '#username' the message gets posted in that user 'slackbot' DM. But I want it to be posted in my bot's DM with that user.
How do I do that?
Or is there an alternative instead of a webhook?
Thanks.
Henry

Direct messages between bot and user
If you want to use a bot-specific direct message channel instead of the general slackbot channel you need to open a direct message channel just as you would between any two users.
Open the direct message channel from your bot to a user with im.open (which will provide you with the channel ID). Then send the message to that channel ID, e.g. with chat.postMessage.
Important: Make sure you use the bot access token and not the general access token for all API calls.
Method for sending messages
I would recommend using the API method chat.postMessage instead of the webhook. It gives you more options than a webhook and of course also supports attachments.

Related

Slack API: Can I get all unread messages for a user?

I develop a mobile app Speaking Email that reads emails out to the user, which can be used while driving to work for example. I would like to add Slack as an account type. Ideally we would have Speaking Email read out the user’s slack messages that are unread, as these will be what they need to catch up on. However it does not appear the Slack API has a method for retrieving the messages the user hasn’t read. Is this possible with the latest Slack API?
I was searching for the same thing and found their API doc about Read Messages!
https://api.slack.com/messaging/retrieving

How to receive contact name via Twilio WhatsApp

I am using the Twilio API for WhatsApp in a sandbox, for prototyping and testing. According to the Facebook / WhatsApp documentation, there are several types of inbound notifications that may be delivered to your unique Webhook.
Twilio API documentation is very limited and doesn’t mention any of the more advanced scenarios. For example retrieving the customers name, which should be present in every webhook request…
When I inspect the webhook, the body payload is very limited. Is there any way to receive the original channelData?
Apparently, this feature is not implemented yet. I received the following statement from Twilio.
Unfortunately we don't support the Contact name being included in the inbound Whatsapp message posted to your webhook at this time. I've raised a feature request for this with the Product Team in hopes that we'll have this feature available in the near future.
You can get the sender's name using the 'ProfileName' key from the request form.

Show system messages in conversation history using iOS Twilio SDK

Hi I have been working on one of my apps for integrating chat using Twilio SDK. I would want to know if it is possible to get the system message inside a channel for eg: 'Member A joined the channel', 'Member B left the channel', 'Member C accepted the invitation to join' and so on. It seems like Twilio SDK does not have API to get the system messages or to set them. How can we achieve this?
I also noticed that it can be achieved by setting the custom key-value pair in attributes inside the message and sending it to the channel. But for the newly invited members, they cannot send a message when they are declining the invitation request such as 'Member A declined the invitation to join channel', as they have not joined the channel yet.
Twilio developer evangelist here.
If you want to send persistent system level messages like this, you can do so with a combination of webhooks and the REST API.
The idea is that you can send messages to a channel using the REST API whenever you need one of these system messages. The default user for the REST API is the system and you can then treat messages from system as special for display in your UI.
I mention the webhooks because you can register to receive them for a number of useful events, like members joining and leaving channels. The webhooks may not cover all the things you want, like declining an invite, but for that I would just trigger my own HTTP request from the application to your server.
Let me know if that helps at all.

Creating a Slack Webhook programmatically

Is there a way I can use the Slack API to create an incoming and outgoing webhook for a channel I just created?
No, I do not think that feature exists, but you can just use any existing webhook with the newly created channel or use the Event API to get the same result.
Incoming Webhooks
You can redirect an incoming webhook to any channel by providing the channel name as parameter in your call. (see Channel override)
Outgoing Webhooks
Your existing outgoing webhook will work with any channel, as long as you use triger words when you created them. If that is not an option, I would recommend to look at the new Event API that would allow you to get the same functionality as with outgoing webhooks, but without having to provide a channel name in the first place. Check out the event types message.channels and message.groups.

How to send message to Slack listening channels?

i am working on a slack app (scope including bot and incoming webhooks). I can send message to a defined channel but i don't know how to stop using the "&channel=" parameter and just send messages to listening channels. By listening i mean, when the app is installed, user is asked where to post (channel or dm has to be chosen).
String postUrl = "https://slack.com/api/chat.postMessage?token=" + botAccessToken + "&as_user=false&channel=%23community&text=testing&username=CommunityQ";
Any hints would be useful.
I think you can't: According to the official Slack API documentation it is not possible to send messages to all/multiple listening channels:
Incoming webhooks output to a default channel and can only send
messages to a single channel at a time. You can override a custom
integration's configured channel by specifying the channel field in
your JSON payload.
I interpret this as "there is always exactly one channel your message is sent to"
Furthermore, Slack restricts this channel override feature for Slack apps:
You cannot override the default username, icon, or channel for
incoming webhooks attached to Slack apps.
I think there is a slight confusion here. I am not sure what the "incoming webhook" scope does that the "bot" scope cannot do. Here's how I see things
Either you want a lightweight, low-permission app that posts in one channel, and you'd use the incoming webhook scope
Or you want an app that can ask questions to the users, like which channels they'd like to have updated posted to, process answers, etc. Then you'd use the bot scope, and the bot can post in any public channel
If you give a bit more details in what you want to achieve we can perhaps help you better
If you want a more dynamic approach on sending messages into any channel I would suggest to user the API method chat.postMessage instead of an incoming webhook.
The API method can post messages into any channel (including private channels, DMs) as long as your token as access to it.
As Michael mentioned in his answer, it is not possible to post to multiple channels at the same time. You have to look through them and do multiple requests.

Resources