Creating a Slack Webhook programmatically - slack-api

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.

Related

Get Whatsapp Number of user in Twilio autopilot

I am trying to create a chatbot using Twilio Autopilot which integrated with WhatsApp. So my flow is like, if user messages on registerd number, Twilio WhatsApp senders webhook will get trigger and it will call Autopilot chatbot task. Task will process the data and it will send reply.
In all above flow I want to capture the WhatsApp number of the user who is asking the question. that number I want in autopilot, so Autopilot will send that number to Twilio function and I want to save that number in my Database using external API's call from Twilio function.
How I will get WhatsApp number inside Twilio Autopilot chatbot for user who is asking questions?
Thanks in Advance
Autopilot can respond using static actions defined within the task, but it can also send a webhook request to fetch Actions JSON from your server. That webhook request contains details about the user you are talking with, including the parameter UserIdentifier which, in a WhatsApp chat, will be the user's WhatsApp number.
If you are currently responding to tasks from Actions JSON defined in the Twilio console, you can add a webhook by using a "redirect" action at the end of a set of actions to a URL you control and then responding to that with a "listen" action to wait for the next task. Alternatively, you could just set a URL for the webhook and respond with your Actions JSON from your own app.

Triggering a Twilio function to write a record in an external service (e.g. a CRM) whenever an sms message is SENT via Twilio

What the title describes can already be done easily via programs like Zapier, so I am hopeful that it can be done through Twilio functions directly.
Essentially, I have my CRM set up so that based on certain triggers, it sends a http POST request to the Twilio URL that controls SMS sending:
https://api.twilio.com/2010-04-01/Accounts/ + twilioAccSId + /Messages.json
For the most part, this works great!
I just want a way for Twilio to then respond by using a function to write a record into the CRM (using the CRM's APIs).
I would want the record written to include things like the delivery status of the message e.g. whether it failed, or was successful). I'm also open to exploring other ways where Twilio can transmit this outbound message info to the CRM through other means, and then having a function on the CRM then write a record!
Do you have the code to share of what you are attempting and the issue encountered? Or is the question more if this is possible? It is certainly possible.
You will use a statusCallback URL of your outbound POST to the messages resource, which will fire with the statusCallback messages and make an API call to your CRM to update the customer record accordingly. You can append a URL query parameter to your statusCallback, so you know which customer record to update.
Track Delivery Status of Messages
How to Share Information Between Your Applications
Passing Custom Information via Requests to Twilio

Slack webhook and direct/private messages

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.

Getting all event of a Bot(user) via Webhook

I have read about the outbound webhook and Event api. And i am able to install a bot to a team using slack button. outbound webhook dosent fit in my scenario i think.
So how can i can get all events(specially whenever this bot receive a msg) using the event api to my webhook.?
Any direction or right way to solve this problem ..?
Outgoing Webhook and Event API are two completely unrelated functions. So you can not get event notifications through an outgoing webhook.
To get event notification your Slack App need to
request the correct oath scopes
subscribe to the events you want to get in the Slack App config
have a script ready to receive the event requests from Slack
Your script will then be called every time the subscribed event occurs.
For all details please have a look at the excellent official documentation about the Event API.

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