Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

Memo on how to create a Slack Bot (bot)

In addition to I looked up how to use the Slack API.," I also created a bot that goes a little further, so here is a memo of the procedure.

At first, I thought it would be like an extension of the API, but creating a bot was not as easy as with the API....

Because of the length of this article, this section focuses on the overview and GUI operation of the Slack Bot, and the implementation is divided into separate articles.

www.ekwbtblog.com

What is a Slack Bot?

Slack has not only an API function to control Slack from the outside, but also a function to catch user actions such as posting messages and notify them externally from Slack.

The Slack Bot is the window for receiving user actions.

When you create a Bot in Slack, the Bot will appear in Slack as a user. When you send a message to the bot, Slack will send information about the message as an event to the URL you have registered in advance.

Then, based on the event information received at the URL, the bot developer can perform arbitrary processing to command the bot to launch some external service, or to input a message to the bot and have the bot respond to the user.

In other words, a Slack Bot is also a trigger entry window for webhooks.

What you need to create a bot

A URL for receiving events from the bot is required and must be provided by the bot developer. This alone is already a hassle....

In addition, the following behavior must be implemented in the URL

  • When you register a URL, you will receive a message from Slack to check if the URL is official or not.
  • A system that responds within 3 seconds after receiving an event from a bot

If an event arrives and you cannot respond within 3 seconds, it will be considered an error and you will have to retry. Therefore, it is necessary to implement the separation of response and actual tasks, such as only replying to an event when it is received and actually processing it later.

Hmmm....troublesome.... I almost broke down reading the manual....

However, if we stop here, we will definitely forget about it, so we decided to just make a simple bot for now and keep it as a template for the bot.

procedure

Creating Apps

As with When the Slack API, first create an application.

A bot, API token, event notification URL, etc. are associated with each app.

Creating Bot Users

  • [App Settings Page]-[Bot Users].

Set the Always Show My Bot as Online checkbox to "On" so that input is always accepted from the bot.

Memo on how to create a Slack Bot (bot)

Register URL to receive events

At this point, the URL must be ready; it must be real and implemented so that it can answer responses from Slack.

  • Go to [App Settings Page]-[Event Subscriptions] page
  • [Check the "Enable Events" checkbox to "On".
  • Register the "URL" to receive the event in the [Request URL] field.

Memo on how to create a Slack Bot (bot)

When you enter a URL, Slack will send the following data to that URL via "POST".

{
    "token": "xxxx",
    "challenge": "xxxx",
    "type": "url_verification"
}

It checks if the token is the same as the "Verification Token" of the application, and if it is the same, it returns the "challenge" sent to it with the following response.

{
    "challenge": "xxxx",
}

The "Verification Token" can be obtained from the "Basic Information" page of the application settings.

Memo on how to create a Slack Bot (bot)

Slack will then register the URL as correct and allow you to register what events will be sent to that URL when they occur.

If an event arrives at the same URL and the "type" is "event_callback" for an event, you must look at the "type" sent and identify whether it is a URL confirmation message or an event message.

Register for the event to receive

Now that the URL has been authenticated, the next step is to register what events you would like to receive from the bot.

[Subscribe to Workspace Event] is an event from the entire workspace. [Subscribe to Bot Events] are events related to the Bot, such as remarks to the Bot.

I think [Subscribe to Bot Events] is usually sufficient.

Memo on how to create a Slack Bot (bot)

Installing Apps

This completes the configuration for sending an event to the URL when an action is performed on the bot.

To actually receive the event, install the application on your workspace.

  • [App Settings Page]-[Install App]-[Install App to Workspace].

Install the software with This is the same as for the Slack API.

Memo on how to create a Slack Bot (bot)

Then, as with the Slack API, a user token and a bot token will be issued.

Memo on how to create a Slack Bot (bot)

When you receive an event from a bot and want to perform an operation on Slack, such as replying to the event, you can use these Tokens to call the Slack API.

Authentication/Response

Now, when an action is performed on the Bot in Slack, the Bot will actually send an event to the URL.

Authentication Method

Each time the event's "Verification Token" and "api_app_id" are checked to see if they are the same as those listed in the app, which is assumed to be authentication.

Verification Token" and "api_app_id" can be found in [App Settings Page]-[Basic Information]-[App Credentials].

Memo on how to create a Slack Bot (bot)

response

As mentioned earlier, when an event is delivered to the URL, it must respond with code 200 within 3 seconds.

So, when you receive some event, regardless of the content, you should try to respond immediately with a code 200.

The above is an overview of the Slack Bot (bot) and notes on GUI operation.

Implementation is discussed in a separate article.

www.ekwbtblog.com

The Slack API is also a separate article.

www.ekwbtblog.com

impressions

Slack's site looks stylish at a glance, but it is still confusing....

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com