LoginSignup
4
2

More than 5 years have passed since last update.

[Bitcoin]トランザクションIDを送ると確認数を返すLINE BOT

Posted at

概要

スクリーンショット 2017-01-08 11.10.47.png

ユーザーから送られてきたトランザクションIDをもとにbitFlyerのchainFlyer APIを使ってトランザクション情報→確認数を取得、返信

コード

callback.rb
require 'sinatra'
require 'line/bot'
require 'json'
require 'open-uri'

def client
  @client ||= Line::Bot::Client.new { |config|
    config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
    config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
  }
end

post '/callback' do
  body = request.body.read
  signature = request.env['HTTP_X_LINE_SIGNATURE']
  unless client.validate_signature(body, signature)
    error 400 do 'Bad Request' end
  end
  events = client.parse_events_from(body)
  events.each { |event|
    case event
    when Line::Bot::Event::Message
      case event.type
      when Line::Bot::Event::MessageType::Text
        begin
          #ユーザーから送られてきたメッセージ
          events_message_text = JSON.parse(body)["events"][0]["message"]["text"]
          #送られてきたメッセージ=トランザクションIDをエンドポイントURLに結合し、トランザクションの情報を取得
          transaction_info = open("https://chainflyer.bitflyer.jp/v1/tx/"+events_message_text).read
          #確認数
          confirmed = JSON.parse(transaction_info)["confirmed"]
          message = {type:'text',text:confirmed}
        rescue
          #送られてきたメッセージがトランザクションIDでではない場合の処理
          message = {type:'text',text:"トランザクションが見つかりません"}
        end
        client.reply_message(event['replyToken'], message)
      end
    end
  }

  "OK"
end
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2