# Building a bot

This guide describes how to use the Messaging API to build a LINE bot.

# Before you begin

Make sure you have completed the following:

# Setting up your bot on the LINE Developers Console

Your bot app requires a channel access token to make API calls and a webhook URL to receive webhook payloads from the LINE Platform.

# Issue a channel access token

Channel access tokens are access tokens that are used for the Messaging API. You can issue any of these tokens:

# Set a Webhook URL

The webhook URL is the endpoint of your bot server where webhook payloads are sent.

  1. Log in to the LINE Developers Console, and click the provider in which the channel for the Messaging API is present.

  2. Click the channel for the Messaging API.

  3. Select the Messaging API tab.

  4. Click Edit under Webhook URL, enter the Webhook URL (the destination URL for events sent from the LINE Platform to the bot), and then click Update.

    The webhook URL must use HTTPS and have an SSL/TLS certificate issued by a certificate authority widely trusted by general web browsers. Also note that self-signed certificates aren't acceptable. If you encounter issues related to your SSL/TLS configuration, you should check whether your SSL/TLS certificate chain is complete and whether your intermediate certificates are correctly installed on your server.

  5. Click Verify.

    If the webhook URL accepts requests, Success is displayed.

  6. Enable Use webhook.

Messaging API settings > Webhook settings > Webhook URL

# Add your LINE Official Account as a friend

Add the LINE Official Account associated with the channel for your bot as a friend on LINE. To do so, scan the QR code on the Messaging API tab in the LINE Developers Console.

# Configure security settings (optional)

To improve security, you can specify servers that can call APIs of the LINE Platform on the Security tab of your channel settings in the LINE Developers Console. You can register IP addresses individually or, if you have multiple servers, you can use classless inter-domain routing (CIDR) notation to register your network address.

[Security settings] tab

Note

This feature is only applicable for API requests using long-term channel access tokens.

# Confirming the webhook behavior

When a user interacts with your LINE Official Account such as adding the LINE Official Account as a friend or sending the LINE Official Account a message, the LINE Platform sends an HTTP POST request that contains a webhook event object to the bot server specified in the Webhook URL field on the Messaging API tab. The request header contains a signature.

This section explains how to check if your server can receive webhook events and how to validate the signature of webhook events.

# Receiving webhook events

To confirm that your server can receive webhook events, block your LINE Official Account on LINE and check your server logs to confirm that your bot server receives an unfollow event from the LINE Platform. The following is an example log.

2017-07-21T09:18:46.755256+00:00 app[web.1]: 2017-07-21 09:18:46.737  INFO 4 --- [io-13386-exec-2] c.e.bot.spring.KitchenSinkController     : unfollowed this bot: UnfollowEvent(source=UserSource(userId=Uxxxxxxxxxx...), timestamp=2017-07-21T09:18:46.031Z)

After you have confirmed that the webhook works normally, add your LINE Official Account as a friend again.

# Setting with LINE Official Account Manager

LINE Official Account Manager (opens new window) is a tool for managing your LINE Official Account. In addition to using features provided by the Messaging API, you can improve the user experience by customizing your profile, creating LINE VOOM posts, and using other features of LINE Official Account Manager.

For a complete list of features available to LINE Official Accounts, see the LINE for Business (opens new window).

The settings for greeting messages and auto-reply messages

If the settings for Greeting messages and Auto-reply messages are set to Enabled in the Messaging API Settings tab of the channel, the LINE Official Account will automatically respond when a user adds the account as a friend or sends a message to the LINE Official Account. The default setting for Greeting Message and Auto-reply messages is Enabled when the channel is created.

If you don't want greeting and response messages to be sent automatically because the response process is handled by the Messaging API, set the Greeting Messages and Auto-reply messages settings to Disabled in the LINE Official Account Manager (opens new window).

You can use both together, such as using greeting messages to respond when a user adds your LINE Official Account as a friend, and using the Messaging API for other responses. However, developers themselves may be confused whether the message sent to the user is a response from a greeting message or response message, or a response from a bot using the Messaging API. When creating a LINE bot using the Messaging API for the first time, it is recommended to set the Greeting messages and Auto-reply messages settings to Disabled.

# Customize your profile

Your profile is used to set the basic information about your LINE Official Account. The information set here is displayed to the user.

Go to LINE Official Account Manager to add basic information about your LINE Official Account. You can customize the profile photo, cover photo, buttons, and plug-ins.

For more information about customizing your profile, see Profile (opens new window) (only available in Japanese) in LINE for Business.

# Set a greeting message (optional)

On the Messaging API tab of your channel settings on the LINE Developers Console, under Greeting messages, click Edit to go to LINE Official Account Manager and set a greeting message to send users when they first add your LINE Official Account as a friend. Alternatively, you can do this programmatically by responding to users after receiving a follow webhook event.

# Set auto reply messages (optional)

On the Messaging API tab of your channel settings on the LINE Developers Console, under Auto-reply messages, click Edit to go to LINE Official Account Manager and set automated reply messages to respond to messages sent by users. However, you can do more with the Messaging API as you can program your bot to reply in different ways to various webhook events.

# Next steps

After you set up your bot, you can receive messages from users or send messages to users from your LINE Official Account. You can also use rich menus and quick replies to create a personalized experience.

For more information about features available within the Messaging API, see the rest of the Messaging API documentation.

If you created your bot for the purpose of handling LINE Things webhook events, follow the instructions in Receiving messages (webhook).