[RUBY] [Rails] Push transmission with LINE Bot

Overview

Push LineBot from Rails It is a guy who sends at any time as shown below Screenshot_20200912-131255.png

Creating a LINE Bot

There are many articles, so please refer to them. For example, there is the following article

The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]

Push from Rails with LINE Bot

  1. Please install the library line-bot-api with Gemfile
  2. ʻENV ["LINE_CHANNEL_SECRET"] and ʻENV ["LINE_CHANNEL_TOKEN "] must be set on the server side such as heroku. I will explain after this
  3. You can check your user_id from Channel Preferences-> Your User ID.
  4. push_message is the part to push with linebot

linebot_controller.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"]  
      }  
    end  
     
    def push
        
        message={
            type: 'text',
            text: "hello"
           }
        user_id =  '[User ID of the destination LINE account]'
        response = client.push_message(user_id, message)
    end

  end  

Deploy Rails app on heroku

There are many articles on deploying Rails apps, please refer to them. For example, there is the following article

Procedure to deploy Rails app on Heroku

At this time, be sure to set channel basic settings-> channel secret, Messaging API settings-> channel access token to heroku as shown below.

$ heroku config:set LINE_CHANNEL_SECRET="[Channel secret]"
$ heroku config:set LINE_CHANNEL_TOKEN="[Channel access token]"

result

Execute the action push of linebot_controller.rb of Rails application deployed on heroku I was able to push "hello" as shown below Screenshot_20200912-131255.png

Recommended Posts

[Rails] Push transmission with LINE Bot
I made a LINE bot with Rails + heroku
Deploy Line bot with rails5 + Docker + Heroku Note
line bot
Conversation status (context) management (session management?) With LINE BOT
Easy to make LINE BOT with Java Servlet
How to push an app developed with Rails to Github
Rails deploy with Docker
[Rails 6] RuntimeError with $ rails s
Let's make a LINE Bot with Ruby + Sinatra --Part 2
Handle devise with Rails
[Rails] Learning with Rails tutorial
[Rails] Test with RSpec
[Rails] Development with MySQL
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Supports multilingualization with Rails!
Double polymorphic with Rails
[Rails] Create an echo bot using the LINE Messaging API.
I want to push an app made with Rails 6 to GitHub
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot
Introduced graph function with rails
[Rails] Express polymorphic with graphql-ruby
[Rails] Upload videos with Rails (ActiveStorage)
Try using view_component with rails
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
Japaneseize using i18n with Rails
API creation with Rails + GraphQL
Preparation for developing with Rails
Run Rails whenever with docker
[Docker] Rails 5.2 environment construction with docker
Use multiple databases with Rails 6.0
[Rails] Specify format with link_to
Login function implementation with rails
[Docker] Use whenever with Docker + Rails