LoginSignup
0
0

More than 3 years have passed since last update.

RailsでLINE_Boot作成する

Last updated at Posted at 2021-04-03

前提

LINE-Botの作成の方法を備忘録として残してます。

環境

  • Homebrow
  • ruby '2.6.3'
  • rails '6.1.3'

Messaging API | LINE Developers

Messaging API | LINE Developers
先にLINE Developers登録してください。

チャネルの作成

「新規プロバイダー作成」をして「Messaging API」をクリックします。

$ gem 'line-bot-api'

bundle installしましょう

ngrokのインストール

Homebrewを使ってngrokをインストールします。

$ brew install ngrok
$ ngrok --version

バージョンが表示されていたらインストール出来ています。

$ rails s

railsのサーバー起動して新しいターミナルでngrokを起動させます。
http://localhost:3000でアクセスしてますのでhttp 3000します。

$ ngrok http 3000

ngrokのターミナル上で表示されていますのでここからアクセスできます。
ですがrailsでDNSリバインディング攻撃の対策のためアクセスできないようになっています。

Forwarding  https://xxxxxxx.ngrok.io
config/environments/development.rb
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker

  # Uncomment if you wish to allow Action Cable access from any origin.
  # config.action_cable.disable_request_forgery_protection = true
  config.hosts.clear #追加します
end

railsを再起動させてngrokのターミナルのhttps://xxxxxxx.ngrok.ioアクセスします。

app/controllers/line_bot_controller.rb
 protect_from_forgery except: [:アクション名]
 #CSRF対策を無効化するコードです。

.env作成

gem 'dotenv-rails'

bundle installします。

$ touch .env
.env
LINE_CHANNEL_SECRET='xxxxxxxx'
LINE_CHANNEL_TOKEN='xxxxxxxx'

LINE_CHANNEL_SECRET
基本設定のチャンネルシークレットを発行し、コピーします。

LINE_CHANNEL_TOKEN
メッセージングAPI設定のチャネルアクセストークンを発行し、コピーします。

gitignore
/.env #追加します

Webhook設定

表示させたページを設定します。
https://xxxxxxxxxx.ngrok.io/xxx
ngrokを起動するたびにURLが変わるため設定しなおす必要があります。

これでRailsとlineの連携の設定は完成です。
簡単な返答くらいなら可能な状態になりました。

gem 'httpclient'を使用し、WebAPI叩ける様にもできます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0