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.
Ruby Ruby On Rails6 Heroku LINE Messager API
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
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
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
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
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'
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.
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