icps

notes

Line-bot

11/18

https://github.com/line/line-bot-sdk-ruby https://devdocs.line.me/en/#webhook-event-object https://business.line.me/zh-hant/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
TO = "UXXXXXXXXXXX"

def client
  client = Line::Bot::Client.new do |config|
      config.channel_secret = "XXXXXXXXXXXXXXXXXXXX"
      config.channel_token  = "XXXXXXXXXXXXXXXXXXXX"
  end
end

def text_msg(text)
  {
    type: 'text',
    text: text
  }
end

def jpg_msg(image)
    {
      type: 'image',
      originalContentUrl: image,
      previewImageUrl: image
  }
end

def map_msg(address,lat,long)
 {
    type: "location",
    title: address,
    address: address,
    latitude: lat,
    longitude: long
  }
end

def sticker_msg(pid,sid)
  {
    type: "sticker",
    packageId: pid,
    stickerId: sid
  }

end

def confirm_msg
 {
  type: "template",
  altText: "this is a confirm template",
  template: {
      type: "confirm",
      text: "要不要去吃火鍋",
      actions: [
          {
            type: "message",
            label: "好",
            text: "好"
          },
          {
            type: "message",
            label: "不好",
            text: "不好"
          }
      ]
  }
}
end
message = "text"
#送訊息
send = client.push_message( TO, message )
#userId
#event['source']['userId']
#個人資料
response = client.get_profile(TO)
case response
when Net::HTTPSuccess then
  contact = JSON.parse(response.body)
  p contact['displayName']
  p contact['pictureUrl']
  p contact['statusMessage']
else
  p "#{response.code} #{response.body}"
end

old

:https://business.line.me/zh-hant/products/4/introduction
https://github.com/line/line-bot-sdk-ruby
https://developers.line.me/bot-api/overview
https://www.heroku.com/

1
2
3
4
5
#routes.rb
Rails.application.routes.draw do
  root :to => "page#index"
  match '/callback' => "page#callback", :via => :post
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#controller.rb
class PageController < ApplicationController
  protect_from_forgery with: :null_session

  def index
  end

  def callback
    line_msg = params[:result][0]
    username = client.get_user_profile( line_msg["content"]["from"] ).contacts[0].display_name
  end

  private

  def client
    Line::Bot::Client.new do |config|
      config.channel_id     = ENV["LINE_CHANNEL_ID"]
      config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
      config.channel_mid    = ENV["LINE_CHANNEL_MID"]
    end
  end
end