I am trying to figure out how to send a direct message to a bot in slack using the Slack API and get the response by the bot. For example, I use chat.postMessage:
curl -X POST -d
'token=xoxp-xxxx-xxxxxxxxx-xxxx&channel=BOT_ID&text=where?&as_user=true'
https://slack.com/api/chat.postMessage
The bot respond with:
I'm at hubot-server1.localI'm at hubot-server2.local
I want to capture the response. What Slack API I can use to capture? chat.postMessage doesn't return it.
Thanks for any reply.
Regards...
There are no API methods that will directly return a response from a bot. To capture responses of bots (and users) you need to read the messages from a channel.
With the Web API you can do that by calling channels.history for public channels (or groups.history private channels and similar names methods for other conversation types. you can also use the new conversations.history method to access all types of channels).
Those methods will give you all messages of a channel and you will need to filter out the right message within your app.
You can also get all new messages for a channel with the RTM API and the Events API.
Related
I am trying to use Twit to send and recieve Twitter DMs.
When I create a stream like this:
var stream = T.stream('user');
I get a 404 error. I believe this is because the user stream has been deprecated.
What stream can I use instead to send and receive DMs?
That's correct, you will need to use the Account Activity API to receive Direct Messages in realtime. This is a webhook-based API. Twitter has a migration guide.
In summary:
Create an app with a listening URL for Twitter to post events to.
Register the webhook
Listen for incoming Direct Messages
Use the REST Direct Message API to respond.
There's also a full tutorial on this using a JavaScript package called autohook.
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.
I'm trying to find a way to send events to a hubot running on Slack from, say, a python script. I've looked at the Events API documentation, but it only talks about how to receive information on events from Slack. Is there any way to emit an event to slack through the API?
No, you can only receive events from Slack with the Events API. But they are many other ways to interact with the Slack using the normal Slack API, e.g. sending messages to a channel.
Using the Slack API (webhooks, RTM, Web, doesn't matter) is it possible to initiate a DM/Private channel with a user where the private IM channel doesn't already exist?
I am aware of the API call https://api.slack.com/methods/im.list which will show me the private IM channels that are already open, however this is an empty list for the bot as no real user will have DM'd the bot.
Basically, we have a list of users who need to be notified of something privately by a bot, and those users will not likely have DM'd the bot before, so the private channel will not yet exist. How can we create that channel using the API?
Well, the fool that I am just needed to RTFM.
https://api.slack.com/methods/im.open
This method opens a direct message channel with another member of your Slack team.
Arguments
This method has the URL https://slack.com/api/im.open and follows the Slack Web API calling conventions.
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.