[RUBY] Deploy Line bot with rails5 + Docker + Heroku Note

Target

--Building Ruby on Rails5 (RoR) development environment with docker-compose --Deploy to Heroku --Create a new channel with LINE DV ELOPERS --Creation of parrot return BOT

Prerequisites

RoR development environment construction

$ mkdir Linebot
$ cd Linebot
$ vi Dockerfiles

Dockerfile


# ruby2.3.Execute the following command on the image layer of 3 Declaration
FROM ruby:2.3.3
# apt-get is like a package manager
#Update list with update & install required packages
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

#Create and set working directory
RUN mkdir /app_name 
##APP the working directory name_Assign to ROOT and$APP_Referenced by ROOT
ENV APP_ROOT /app_name 
WORKDIR $APP_ROOT

#Add host side (local) Gemfile
ADD ./Gemfile $APP_ROOT/Gemfile
ADD ./Gemfile.lock $APP_ROOT/Gemfile.lock

#Gemfile bundle install
RUN bundle install
ADD . $APP_ROOT
$ vi docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: postgres:13.0
    environment:
      POSTGRES_PASSWORD: 'postgres'
    ports:
      - "3306:3306"

  web:
    build: .
    command: rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app_name
    ports:
      - "3000:3000"
    depends_on:
      - db

Start a container called web and db

$ touch Gemfile.lock
$ vi Gemfile

Gemfile


source 'https://rubygems.org'
gem 'rails', '~> 5.2.2'
$ docker-compose run app rails new . --force --database=postgresql --skip-bundle
$ docker-compose build -d
$ docker-compose up -d web

The -d option boots in detach mode (runs on the backend without logging) If you want to see the log, you can remove it, but if you stop it, the container will also stop, so it is troublesome

/config/database.yml


default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: db
  username: postgres
  password: postgres
....
production:
  <<: *default
  database: app_name_production
  username: postgres
  password: <%= ENV['APP_NAME_DATABASE_PASSWORD'] %>

Write that postgresql will be used in development and production When using Heroku, which will be described later, postgresql is the standard, so it's good. Well, it's okay to use MySQL

Gemfile


#For LINE API
gem 'line-bot-api'

#For managing access tokens, etc.(Environment variable management)
gem 'dotenv-rails'

#postgresql for heroku environment
gem 'pg'
$ docker-compose run web bundle install
$ docker-compose run web rails db:create
$ docker-compose run web rails g controller top index
$ docker-compose run web rails g controller linebot collback

I'll get angry if I don't create a database Since you need a top screen on Heroku, let's make anything for the time being (I don't think it's good to play with volume directly, not just commands, but ignore it)

routes.rb


post '/callback' => 'linebot#callback'
root 'top#index'

/app/controllers/linebot_controller


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

  #Disable CSRF token authentication for callback action
  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)
      head :bad_request
    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)
        when Line::Bot::Event::MessageType::Image, Line::Bot::Event::MessageType::Video
          response = client.get_message_content(event.message['id'])
          tf = Tempfile.open("content")
          tf.write(response.body)
        end
      end
    }

    head :ok
  end
end
$ touch .env

.env


LINE_CHANNEL_SECRET=XXXXXXXXXXX
LINE_CHANNEL_TOKEN=XXXXXXXXXXX

For XXXXXXXXXXX, write the Line Developers guy you'll create later. If you want to drop a local container $ docker-compose down OK if you do it with If you drop it with Ctl + C $ rm tmp/pids/server.pid It is better to do

Deploy to Heroku

$ vi heroku.yml

heroku.yml


build:
  docker:
    web: Dockerfile
run:
  web: bundle exec puma -C config/puma.rb

You can't build without writing this It seems that heroku.yml can switch development / production and the accompanying db (not done)

$ heroku login
$ heroku create
$ heroku addons:create heroku-postgresql:hobby-dev
$ heroku container:login
$ heroku container:push web
$ git init
$ heroku git:remote -a <Your heroku repository name>
$ git add .
$ git commit -am "Nice comment"
$ heroku stack:set container
$ git push heroku master
$ heroku container:release web
$ heroku open

If you can't cut the branch, you can add it like git, commit it, and update it with heroku push. Around here, container push and image push are cluttered, so it may be better not to imitate too much

――When something is wrong with the server $ heroku restert You should try hitting --When you want to check the log $ heroku logs --tail Deo k --If you want to see the files on the server $ heroku bash It is good to see it by hitting the cat command with (the point of anger is that vim can not be used)

Connect Line channels and containers

--Create channel with Messaging API --Issue a channel secret and channel access token and paste it into your .env file --Turn off response and greeting messages to enable webhooks --After enabling webhook, enter / callback in webhook URL and verify (if it doesn't succeed, try heroku restart)

bonus

I want to test but I don't want to deploy! I want to publish it locally! You can use ngrok

I want to send a request for verification for the time being! Use Talend API Tester

Articles that were taken care of

LINE Bot Development Super Introduction (Part 1) From Zero to Response https://qiita.com/nkjm/items/38808bbc97d6927837cd

Create a container-based Rails app with Docker Compose and deploy it to Heroku https://qiita.com/akirakudo/items/16a01271b0a39316e439

Rails5 + MySQL on Docker environment construction by too polite Docker-compose (Docker for Mac) https://qiita.com/azul915/items/5b7063cbc80192343fc0

Recommended Posts

Deploy Line bot with rails5 + Docker + Heroku Note
Rails deploy with Docker
Deploy to heroku with Docker (Rails 6, MySQL)
I made a LINE bot with Rails + heroku
[Rails] Push transmission with LINE Bot
Deploy Rails on Docker to heroku
heroku: docker: deploy
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
Deploy with EC2 / Docker / Laravel
Run Rails whenever with docker
[Docker] Rails 5.2 environment construction with docker
[Docker] Use whenever with Docker + Rails
Rails + MySQL environment construction with Docker
Deploy a Docker application with Greengrass
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Deploy Flask's Docker image on Heroku
[Environment construction with Docker] Rails 6 & MySQL 8
Deploy your Rails app on Heroku
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
[Rails] How to use rails console with docker
heroku deploy
Deploy to Heroku [Ruby on Rails] Beginner
Deploy your application with VPC + EC2 + Docker.
line bot
[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
How to build Rails 6 environment with Docker
Introducing Rspec with Ruby on Rails x Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Notes on building Rails6 / PostgreSQL with Docker Compose
Deploy Rails to ECS Fargate with AWS Copilot
Error deploying rails5 + Mysql to heroku with Docker-compose
[Rails] How to build an environment with Docker
Easy to make LINE BOT with Java Servlet
Easy to display hello world with Rails + Docker
A note about getting stuck when making a parrot return LINE bot on Heroku + Sinatra
Rails Credentials Note
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (6)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
Rails Docker ~ Part 1 ~
Rails Docker ~ Part 2 ~
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
How to make batch processing with Rails + Heroku configuration
How to deploy to Heroku from a local docker image
Bundle install with docker + rails6 does not reflect gem
[Heroku] Associate AWS S3 with the deployed Rails app
Let's make a LINE Bot with Ruby + Sinatra --Part 2
Error encountered with notes when deploying docker on rails
[Note] I suddenly can't build with Docker for windows.
[Note] Create a java environment from scratch with docker
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Docker + Rails] How to deal with Rails server startup failure