Class: BotPlatform::Channels::Lineworks

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/bot_platform/channels/lineworks.rb

Instance Method Summary collapse

Constructor Details

#initializeLineworks

Returns a new instance of Lineworks.



7
8
9
10
11
12
13
14
15
# File 'lib/bot_platform/channels/lineworks.rb', line 7

def initialize
  @queue = []
  @api_uri = URI.parse("https://apis.worksmobile.com/r/#{ENV['LINE_API_ID']}/message/v1/bot/#{ENV['BOT_NO']}/message/push")
  @headers = {
    "Content-type": "application/json",
    "consumerKey": ENV['LINE_SERVER_CONSUMER_KEY'],
    "Authorization": ENV['LINE_SERVER_TOKEN']
  }
end

Instance Method Details

#channel_idObject



17
18
19
# File 'lib/bot_platform/channels/lineworks.rb', line 17

def channel_id
  "lineworks"
end

#match_request(headers, body) ⇒ Object



90
91
92
# File 'lib/bot_platform/channels/lineworks.rb', line 90

def match_request(headers, body)
  return !headers["X-Works-Signature"].blank?
end

#parse_incoming_to_activity(headers, body) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bot_platform/channels/lineworks.rb', line 21

def parse_incoming_to_activity(headers, body)
  puts "Headers: #{headers.inspect}"
  puts "body: #{body.inspect}"
  user_id = body["source"]["accountId"] || ""
  room_id = body["source"]["roomId"] || ""
  activity = nil
  if cmd = as_command(headers, body)
    activity = ChatBot::Activity.new ::ChatBot::Activity::TYPES[:command], {from: {user_id: user_id, room_id: room_id}, text: cmd, channel_id: channel_id}
  elsif body["type"] == "message" && body["content"]["type"] == "image"
    activity = ChatBot::Activity.new ::ChatBot::Activity::TYPES[:image], {from: {user_id: user_id, room_id: room_id}, resource_id: body["content"]["resourceId"], channel_id: channel_id}
  else
    activity = ChatBot::Activity.new ::ChatBot::Activity::TYPES[:message], {from: {user_id: user_id, room_id: room_id}, text: body["content"]["text"], channel_id: channel_id}
  end
  puts "parse_incoming_to_activity activity:#{activity.to_json}"
  
  return activity
end

#send_activity(activity) ⇒ Object



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
82
83
84
85
86
87
88
# File 'lib/bot_platform/channels/lineworks.rb', line 39

def send_activity(activity)
  http = Net::HTTP.new(@api_uri.host, @api_uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  content = {}
  if !activity.to[:room_id].blank?
    content["roomId"] = activity.to[:room_id]
  else
    content["accountId"] = activity.to[:user_id]
  end
  case activity.type
  when ChatBot::Activity::TYPES[:message] then
    content["content"] = {
          "type": "text",
          "text": activity.text
    }
  when ChatBot::Activity::TYPES[:image] then
    content["content"] = {
          "type": "image",
          "previewUrl": activity.preview_url,
          "resourceUrl": activity.resource_url,
    }
  when ChatBot::Activity::TYPES[:confirm] then
    content["content"] = {
      "type": "button_template",
      "contentText": activity.text,
      "actions": [
        { "type":"message", "label": "はい", "postback": "#{activity.prefix}-yes"}, 
        { "type":"message", "label": "いいえ","postback":"#{activity.prefix}-no"}
      ]
    }
  when ChatBot::Activity::TYPES[:options] then
    content["content"] = {
      "type": "button_template",
      "contentText": activity.text,
      "actions": activity.options.map{|opt| {"type":"message", "label":opt, "postback": "#{activity.prefix}-opt-#{opt}"}}
    }
  when ChatBot::Activity::TYPES[:carousel] then
    content["content"] = activity.content
  else
  end

  http.start do
    req = Net::HTTP::Post.new(@api_uri.path)
    req.initialize_http_header(@headers)
    req.body = content.to_json
    http.request(req)
  end
end