Module: Janis

Defined in:
lib/janis.rb

Defined Under Namespace

Classes: FBApi, Janiapi

Constant Summary collapse

EVENTS =
[:'chat response', :'socket_id_set', :'channel update'].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.apikeyObject

Return a Hash of hooks.



39
40
41
# File 'lib/janis.rb', line 39

def apikey
  @apikey
end

.clientkeyObject

Returns the value of attribute clientkey.



22
23
24
# File 'lib/janis.rb', line 22

def clientkey
  @clientkey
end

.optionsObject

Returns the value of attribute options.



22
23
24
# File 'lib/janis.rb', line 22

def options
  @options
end

.platformObject

Returns the value of attribute platform.



22
23
24
# File 'lib/janis.rb', line 22

def platform
  @platform
end

.tokenObject

Returns the value of attribute token.



22
23
24
# File 'lib/janis.rb', line 22

def token
  @token
end

Class Method Details

.assistanceRequested(x) ⇒ Object



121
122
123
124
# File 'lib/janis.rb', line 121

def assistanceRequested(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/human', data)
end

.headersObject



59
60
61
# File 'lib/janis.rb', line 59

def headers
    @headers = {'apikey':apikey,'clientkey':clientkey,'platform':platform, 'token': token}
end

.hooksObject



63
64
65
# File 'lib/janis.rb', line 63

def hooks
    @hooks ||= {}
end

.hopIn(x) ⇒ Object



106
107
108
109
# File 'lib/janis.rb', line 106

def hopIn(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/in', data)
end

.hopOut(x) ⇒ Object



111
112
113
114
# File 'lib/janis.rb', line 111

def hopOut(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/out', data)
end

.initializeObject



24
25
26
27
28
29
30
# File 'lib/janis.rb', line 24

def initialize
    @apikey
    @clientkey
    @token
    @platform
    @options
end

.janisappidObject



55
56
57
# File 'lib/janis.rb', line 55

def janisappid
    @janisappid = 1242623579085955
end

.logUnknownIntent(x) ⇒ Object



116
117
118
119
# File 'lib/janis.rb', line 116

def logUnknownIntent(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/unknown', data)
end

.new(*args, &block) ⇒ Object



32
33
34
35
36
# File 'lib/janis.rb', line 32

def new(*args, &block)
    obj = allocate
    obj.initialize(*args, &block)
    obj
end

.on(event, &block) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/janis.rb', line 91

def on(event, &block)
    unless EVENTS.include? event
        raise ArgumentError,
        "#{event} is not a valid event; " \
        "available events are #{EVENTS.join(',')}"
    end
    hooks[event] = block
end

.passThreadControl(x) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/janis.rb', line 126

def passThreadControl(x)
    message = x['message']
    recipientid = x['recipient']['id']
    appid = message['app_id']
    is_echo = message['is_echo']
    if message['is_echo'] && (appid == janisappid || appid.nil?)
        
        # If an agent responds via the Messenger Inbox, then `appId` will be null.
        # If an agent responds from Janis on Slack, the `appId` will be 1242623579085955.
        # In both cases, we should pause your bot by giving the thread control to Janis.
        # Janis will pass control back to your app again after 10 minutes of inactivity.
        # If you want to manually pass back control, use the slash command `/resume`
        # in the Janis transcript channel, or press "Done" in the Page Inbox on the thread.
        
        # See: https://developers.facebook.com/docs/messenger-platform/handover-protocol#app_roles
        # This app should be the Primary Receiver. Janis should be a Secondary Receiver.
        # Every time an echo from either Janis or the Page Inbox is received,
        # this app passes control over to Janis so the humans are the only ones who can respond.
        j = {"recipient": {"id": recipientid}, "target_app_id": janisappid, "metadata": "passing thread"}
        uri = "/pass_thread_control?access_token=" + token
        return FBApi.post(uri, {'body':j})
    end
    return false
end

.trigger(event, *args) ⇒ Object



100
101
102
103
104
# File 'lib/janis.rb', line 100

def trigger(event, *args)
    hooks.fetch(event).call(*args)
rescue KeyError
    $stderr.puts "Ignoring #{event} (no hook registered)"
end