プログラミング

LINE notifyチュートリアル コマンドライン Rubyでの実行 複数グループへの同時投稿

LINE notifyのチュートリアルです。コマンドラインでの実行から、Rubyでの実行、複数グループへの同時投稿を紹介します。

LINE notify トークンの発行方法

トークンを発行する方法です。

まずLINE側で、notifyを利用したいグループを適当に新規作成します。
[get_title https://notify-bot.line.me/my/] > トークンを発行する で、グループを選択します。

進んでいくと、APIキーが表示されます。これをコピーしておきましょう。

LINE notify コマンドラインで実行

LINE notifyをBashのコマンドラインで実行します。

先程取得したAPIキーを、以下のYOUR_API_KEYに埋め込んで、スクリプトを実行してみましょう。

curl https://notify-api.line.me/api/notify -X POST -H 'Authorization: Bearer YOUR_API_KEY' -F 'message=メッセージ。'

メッセージ。とラインが来たら成功です。

LINE notify Rubyで実行

LINE notifyをRubyで実行する方法です。

require 'open3'

@token = 'YOUR_API_KEY'
Open3.popen3("curl https://notify-api.line.me/api/notify -X POST -H \'Authorization: Bearer #{@token}\' -F 'message=#{ARGV[0]}'")

ruby /mnt/f/__HDD_BACKUP__/pg/create_apps/line_notify/notify.rb hogeと第一引数にメッセージを指定して実行します。
これをコマンドに登録しておけば、簡単に実行できるようになり便利です。

LINE notify 複数のグループに送信

以前作ったスクリプトを紹介します。

複数のグループに同時に同じメッセージを送信したい場合に便利です。

README.md


# What? Terminal App of Line notify api. # usage $ ruby line_notify.rb 'Enter your message.' '1' *help* $ ruby line_notify.rb help *methods* \````rb notify=LINE_notify.new notify.verification_argv # if argv is "help" then puts help. notify.confirm # for confirm # notify.message='your good message.' # notify.token=YOUR_TOKEN # notify.token_select='123' # select token number of yaml p notify.help #puts help notify.multi_post \```` *yaml* \````yaml config: keys: group_1: '' #share group_2: '' #dev group_3: '' #notify_3 group_4: '' #notify_4 \````

main.rb


require 'open3' require 'yaml' class LINE_notify attr_accessor :message, :token, :token_select def initialize @config=YAML.load_file(File.expand_path('../config.yaml',__FILE__)) @message,@token_select = ARGV[0],ARGV[1] end def confirm if @token_select.nil? then puts %("argv2 is nil. to exit.");exit elsif @message.nil? then puts %("argv1 is nil. to exit.") ;exit end end def notify @command=%(curl https://notify-api.line.me/api/notify -X POST -H 'Authorization: Bearer #{@token}' -F 'message=#{@message}') Open3.popen3(@command) end #notify end def multi_post 9.times do |index_num| if @token_select.match(/#{index_num}/) @token=@config["config"]["keys"]["group_#{index_num}"] notify() puts %(group_#{index_num}: #{@message}) else puts %(Skip to group_#{index_num}.) end end end def verification_argv if ARGV[0] =~ /help|-h|--h/ then puts help() end end def help open(File.expand_path('../README.md',__FILE__)).read end end

config.yaml

config:
  keys:
    group_1: '' #share
    group_2: '' #dev
    group_3: '' #notify_3
    group_4: '' #notify_4

ctl.rb


require_relative 'main' notify=LINE_notify.new notify.verification_argv # if argv is "help" then puts help. notify.confirm # for confirm # notify.message='your good message.' # notify.token=YOUR_TOKEN # notify.token_select='123' # select token number of yaml # p notify.help #puts help notify.multi_post

簡単な使い方

READMEがわかりにくいので簡単な使い方を紹介します。

まず、ctlはいじらなくていいです。本当はコマンドラインじゃなくて直接実行でもできるようになっているのですが、使わないだろうし。
よって、READMEも読まなくていいです。

大事なのは、config.yamlにAPIキーを記載するということと、引数の書き方です。

引数は、第一引数にメッセージ、第二引数にconfig.yamlに基づいた、メッセージを送信したい番号を羅列します。

ruby ctl.rb メッセージ。 '13'

これだと、1と3のAPIキーに応じたグループにメッセージを送信することになります。

実行すると、こうなります。

yuis@DESKTOP-UHU8FSH:/mnt/c/ContaCam$ ruby /mnt/f/__HDD_BACKUP__/_document/poblic/files/pg/ruby/LINE_notify/ctl.rb hoge 1234
Skip to group_0.
/usr/lib/ruby/2.3.0/open3.rb:199: warning: Insecure world writable dir /home/yuis/.local/bin in PATH, mode 040777
group_1: hoge
group_2: hoge
group_3: hoge
group_4: hoge
Skip to group_5.
Skip to group_6.
Skip to group_7.
Skip to group_8.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です