RubyでLINEnotifyに通知を送ったときのメモ

環境

LINEnotifyでTOKENを発行する

https://notify-bot.line.me/my/ にアクセスして、ログイン。 "トークンを発行する"をクリック。

任意のトークン名を入力し、通知を送信するトークルームを選択したら"発行する"をクリックすると、トークンが発行される。

使用したライブラリ

net/http

library net/http (Ruby 3.1 リファレンスマニュアル)

コード

# frozen_string_literal: true

require 'net/http'
require 'uri'

class LineNotify
  TOKEN = '*************************************'
  URL = 'https://notify-api.line.me/api/notify'

  def send
    Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https|
      https.request(post)
    end
  end

  def post
    request = Net::HTTP::Post.new(uri)
    request['Authorization'] = "Bearer #{TOKEN}"
    request.set_form_data(message: 'メッセージ')
    request
  end

  def uri
    URI.parse(URL)
  end
end

LineNotify.new.send

で成功しました