[Ruby] [Rails] Create echo bot using LINE Messaging API.

2 minute read


We developed LINE Bot to learn Web API. This time we will create an echoBot that will simply return the sent text. Develop with Rails and deploy to Heroku.


・Ruby 2.6.5 ・Rails ・PostgreSQL ・Heroku

table of contents

・Create channel with LINE Developers ・Create Bot program (Ruby/Rails) ・Deploy to Heroku

Create channel with LINE Developers

First, register as a LINE developer. After that, let’s create a provider and a channel and get the required API key. https://developers.line.biz/en/docs/messaging-api/getting-started/

Create a channel for Bot referring to the formula (above). If you break down the work easily,

・Log in to the LINE Developers console ・Enter a name and email address to create a developer account ・Create provider and channel, specify Messaging API at this time ・Issue access token ・Turn off response message and set Webhock on

Make a note of the Channel secret and Channel access token as they are needed for bot development!

Create program for Bot (Ruby/Rails)

Since it deploys on Heroku, DB uses PostgreSQL.

$ rails new app name -d postgresql
$ cd app name
$ git init

install required gems

We use a gem called line-bot-api.


$ bundule install


1. Routing settings


Rails.application.routes.draw do
    post'/callback' =>'linebot#callback'

2. Create controller

$ rails g controller linebot

3. Description on controller

The description on the controller is based on (copy) the RUBY of the line official SDK. https://github.com/line/line-bot-sdk-ruby/blob/master/examples/echobot/app.rb


class LinebotController <ApplicationController
 require'line/bot' # gem'line-bot-api'

def client
  @client ||= Line::Bot::Client.new {|config|
    config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
    config.channel_token = ENV["LINE_CHANNEL_TOKEN"]

post'/callback' do
  body = request.body.read

  signature = request.env['HTTP_X_LINE_SIGNATURE']
  unless client.validate_signature(body, signature)
    halt 400, {'Content-Type' =>'text/plain'},'Bad Request'

  events = client.parse_events_from(body)

  events.each do |event|
    case event
    when Line::Bot::Event::Message
      case event.type
      when Line::Bot::Event::MessageType::Text
        message = {
          text: event.message['text']
        client.reply_message(event['replyToken'], message)


Deploy to Heroku

First, log in to Heroku, create an application on Heroku, and deploy.

Set environment variables on Heroku side.

$ heroku config:set LINE_CHANNEL_SECRET=Paste the Channel Secret you made a note of here
$ heroku config:set LINE_CHANNEL_TOKEN=Paste the access token you made a note of here

Set Webhook settings in LINE developers

Set URL with /callback at the end of deployed address in webhook. image.png

This completes the echo-bot. If you send text, it will return the same text.


Currently, it is not reflected unless you deploy after changing the code, so Next time, I will run it in a local environment using ngrok. https://qiita.com/shizu9d/items/42b2cb209b2e23a9af6d


I have referred to the following article. https://qiita.com/noriya1217/items/00d6461e9f54900377a3