Created the first LINEbot on AWS Cloud9 (Rails + Heroku deployment)

Introduction

Paper, who just finished the Rails tutorial, decided to make LINEbot first. The purpose of this time is to deepen the understanding of "Repeat Bot" that returns characters as they are, using the things that have been learned so far. So, I dare to develop with "Rails + Cloud9 + Heroku" without using convenient things such as GAS.

table of contents

--LINE Developers account & new channel creation --Create a Heroku account --Create Rails app on cloud9 --Version control (Heroku deployment) --Code editing for bots --Check operation with LINE

LINE Developers account & new channel creation

First, log in to LINE Developers. (https://developers.line.biz/ja/) If you are new to LINE, you can easily register with your usual LINE account. Register your provider information and create a new channel. Please refer to this article for details. (Https://qiita.com/nkjm/items/38808bbc97d6927837cd)

Create a Heroku account

If you've already created a Heroku account, skip this and move on to the next step. After opening Heroku, click the new registration button to create an account. (https://jp.heroku.com/) You can do it later, but let's set up Heroku to be available on Cloud9 here. Simply type the following codes into the terminel one by one.

terminal


curl -OL https://cli-assets.heroku.com/heroku-linux-x64.tar.gz
tar zxf heroku-linux-x64.tar.gz && rm -f heroku-linux-x64.tar.gz
sudo mv heroku /usr/local
echo 'PATH=/usr/local/heroku/bin:$PATH' >> $HOME/.bash_profile
source $HOME/.bash_profile > /dev/null

If you can log in successfully, it's OK. You can now use Heroku in your own development environment.

Create an app with Rails on cloud9

If you have learned Rails, let's create an app quickly with the familiar "Rails new".

terminal


rails new repeat-bot

Then edit the Gemfile a bit.

Gemfile


gem 'line-bot-api'
group :development do
  gem 'sqlite3'← This is the newly added part
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
group :production do
 gem 'pg'
end
end

Let's install the newly added Gem.

terminal


bundle install --without production

Version control (Heroku deployment)

Now that you've reached this point, let's manage the version with Git once.

terminal


git init

Next, let's connect the heroku app with the app on Rails.

terminal


heroku git:remote -a heroku app name

If you registered the Heroku app name earlier, enter that name. If you have not changed it, alphanumeric characters are automatically listed, so enter it. (Check from Heroku page)

Then deploy to the Heroku you registered earlier.

terminal


git add .
git commit -m "init"

Finally push and finish.

terminal


git push heroku master

Code editing for bots

Repeatedly add code for the bot. First, create a controller that you are familiar with in Rails.

terminal


rails g controller linebot

Next, copy the channel secret and access token from the LINEbot management screen created this time and paste them into the terminel. Please fill in as follows.

terminal


heroku config:set LINE_CHANNEL_SECRET=Your channel secret
heroku config:set LINE_CHANNEL_TOKEN=Your channel token

Edit the code so that it responds repeatedly. Please copy and paste.

routes.rb


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

linebot_controller.rb


class LinebotController < ApplicationController
  require 'line/bot' 

  protect_from_forgery :except => [:callback]

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

  def callback
    body = request.body.read

    signature = request.env['HTTP_X_LINE_SIGNATURE']
    unless client.validate_signature(body, signature)
      error 400 do 'Bad Request' end
    end

    events = client.parse_events_from(body)

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

    head :ok
  end
end

Finally, let's deploy again.

terminal


git add .
git commit -m "add linebot_controller"
git push heroku master

Then, from the LINEbot management screen, change the Webhook URL to [https://heroku app name.herokuapp.com/callback] Enter with and you're done.

Check operation with LINE

Finally, let's actually enter characters on LINE and see if the response is correct.

Recommended Posts

Created the first LINEbot on AWS Cloud9 (Rails + Heroku deployment)
Cloud IDE: Heroku couldn't be installed in the Ruby on Rails tutorial
What to do if the rails server doesn't run out on AWS cloud9
[Rails] Heroku deployment flow
Build a Ruby on Rails development environment on AWS Cloud9
[Heroku] Associate AWS S3 with the deployed Rails app
Update RVM on AWS Cloud9
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
Rails (postgeresql. Ubuntu environment), heroku deployment
Run the AWS CLI on Docker
Launch Rails on EC2 (manual deployment)
rails AWS deployment is not reflected
[Today's stack # 1] Until on rails on cloud9
Deploy Rails on Docker to heroku
Check the root on the Rails browser
[Rails] AWS deployment error encounter summary
Deploy your Rails app on Heroku
Creating an app and deploying it for the first time on heroku
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
How to redo a deployment on Heroku
Rails: 7 basic actions defined on the controller
Easy deployment with Capistrano + AWS (EC2) + Rails
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
part of the syntax of ruby ​​on rails
Deploy to Heroku [Ruby on Rails] Beginner
Build Java 8 development environment on AWS Cloud9
Try the Docker environment on AWS ECS
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API