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.
--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
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)
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.
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
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
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.
Finally, let's actually enter characters on LINE and see if the response is correct.
Recommended Posts