[RUBY] I made a LINE bot with Rails + heroku

What I made

I thought it would be convenient if there was a BOT that would notify me of garbage disposal every morning. I made it after studying.

スクリーンショット 2020-10-28 23.01.21.png

Language used

Ruby Ruby On Rails6 Heroku LINE Messager API

STEP 1 Create a LINE BOT that returns a message

I will omit this because other people have posted it in detail.

Reference article

https://qiita.com/y428_b/items/d2b1a376f5900aea30dc

https://qiita.com/natsukingdom-yamaguchi/items/e84dffdd90d7f5ef8224

STEP 2 PUSH message

Add a task and send a message at a fixed time.

Creating a task

$rails g task reminder_task

Then generate a controller

$rails g controller webhook trash

describe description and task

lib/tasks/reminder_task.rake


namespace :reminder_task do
  desc "Here is the subject of the task"
  task :trash => :environment do
    webhook = WebhookController.new
    puts webhook.trash
  end
end

Next, define the action of the webhook controller

app/controllers/webhook_controller.rb


  def trash
    puts "message"
  end

Check if it is added to the rake task here

$ rake -T
rake reminder_task:trash task theme

If you can confirm that it has been added

$ rake reminder_task:trash

OK when [Message] is sent

Setting environment variables

If you want to use PUSH messages, you have to set environment variables.

See below for environment variables. https://qiita.com/yuichir43705457/items/7cfcae6546876086b849 https://qiita.com/noraworld/items/bfa80811c9e30b4474af

Setting method

Add dotenv to gem

gem 'dotenv-rails'

Create an .env file in the root directory of your application directory (Created where there is an app or gemfile)

Set various LINE channels

/.env


LINE_CHANNEL_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
LINE_CHANNEL_TOKEN='yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
LINE_USER_ID='zzzzzzzzzzzzzzzzzzzzzz'

Rewrite the trash action of the webhook controller

app/controller/webhook_controller.rb


class WebhookController < ApplicationController
  require 'line/bot'

def trash
  message = {
            "type": "text",
            "text": WebhookController.contents
           }
            client = Line::Bot::Client.new { |config|
              config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
              config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
           }
            response = client.push_message(ENV["LINE_USER_ID"], message)
  end

  def self.contents

      date = Date.today

      case date.strftime('%a')
        when "Mon"
            "Today is Monday, Burning Garbage Day!"
        when "Tue"
            "None!"
        when "Wed"
            "Today is Wednesday, the day of non-burnable garbage and cardboard"
        when "Thu"
            "Today is Thursday, a normal garbage day!"
        when "Fri"
            "Today is Friday, the day of cans, bottles and PET bottles!"
        when "Sat"
            "Tired for a week! None today!"
        else
            ""
        end
    end
  end

Push to Heroku and set up a scheduler and you should get a message.

Finally

The service you made for the first time after studying ruby? Therefore, it seems that there are many mistakes. In that case, I would be grateful if you could point out!

(Thanks to all the reference articles.)

Recommended Posts

I made a LINE bot with Rails + heroku
[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
Deploy Line bot with rails5 + Docker + Heroku Note
I made a portfolio with Ruby On Rails
[Rails] Push transmission with LINE Bot
I made a development environment with rails6 + docker + postgreSQL + Materialize.
I made a risky die with Ruby
I made a rock-paper-scissors app with kotlin
I made a rock-paper-scissors app with android
I made a command line interface with WinMerge Plugin using JD-Core
[Rails] I made a simple calendar mini app with customized specifications.
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
I made a gender selection column with enum
[Rails] I made a draft function using enum
Let's make a LINE Bot with Ruby + Sinatra --Part 2
[Ruby] I made a crawler with anemone and nokogiri.
[Beginner] I stumbled upon launching a project with Rails6
Let's make a LINE Bot with Ruby + Sinatra --Part 1
I made a chat app.
I made a Japanese version of Rails / devise automatic email
I want to push an app made with Rails 6 to GitHub
I made a plugin to execute jextract with Gradle task
I made a mod that instantly calls a vehicle with Minecraft
I came across a guy with two dots in Rails
Ruby: I made a FizzBuzz program!
I made a shopify app @java
[Rails] Creating a new project with rails new
I made a simple recommendation function.
I made a matching app (Android app)
I made a package.xml generation tool.
[Android] I made a pedometer app.
I want to make a button with a line break with link_to [Note]
I want to add a browsing function with ruby on rails
I made a reply function for the Rails Tutorial extension (Part 1)
I got a warning message with the rails _6.0.3_ new hello_myapp command
I made a simple search form with Spring Boot + GitHub Search API.
I made a reply function for the Rails Tutorial extension (Part 5):
I tried to make a group function (bulletin board) with Rails
I made a SPA with Rails + Nuxt.js for half a year of self-study, so please take a look.
I built a rails environment with docker and mysql, but I got stuck
[Ruby] I made a simple Ping client
[Rails6] Create a new app with Rails [Beginner]
Deploy heroku with Rails6 (Cloud9 + Ubuntu) + MySQL
I made an app to scribble with PencilKit on a PDF file
Deploy to heroku with Docker (Rails 6, MySQL)
[Rails withdrawal] Create a simple withdrawal function with rails
I made a virtual currency arbitrage bot and tried to make money
I tried playing with BottomNavigationView a little ①
Make a login function with Rails anyway
I made a plugin for IntelliJ IDEA
I made a calculator app on Android
[Rails 5] Create a new app with Rails [Beginner]
Make a site template easily with Rails
I made a new Java deployment tool
line bot
I made a bulletin board using Docker 1
I made a reply function for Rails Tutorial extension (Part 2): Change model
I made a site that summarizes information on carbohydrate restriction with Vue.js
[LINE @] I tried to make a Japanese calendar Western calendar conversion BOT [Messaging API]
I want to be able to read a file using refile with administrate [rails6]