Module: Janis
- Defined in:
- lib/janis.rb
Defined Under Namespace
Constant Summary collapse
- EVENTS =
[:'chat response', :'socket_id_set', :'channel update'].freeze
Class Attribute Summary collapse
-
.apikey ⇒ Object
Return a Hash of hooks.
-
.clientkey ⇒ Object
Returns the value of attribute clientkey.
-
.options ⇒ Object
Returns the value of attribute options.
-
.platform ⇒ Object
Returns the value of attribute platform.
-
.token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
- .assistanceRequested(x) ⇒ Object
- .headers ⇒ Object
- .hooks ⇒ Object
- .hopIn(x) ⇒ Object
- .hopOut(x) ⇒ Object
- .initialize ⇒ Object
- .janisappid ⇒ Object
- .logUnknownIntent(x) ⇒ Object
- .new(*args, &block) ⇒ Object
- .on(event, &block) ⇒ Object
- .passThreadControl(x) ⇒ Object
- .trigger(event, *args) ⇒ Object
Class Attribute Details
.apikey ⇒ Object
Return a Hash of hooks.
39 40 41 |
# File 'lib/janis.rb', line 39 def apikey @apikey end |
.clientkey ⇒ Object
Returns the value of attribute clientkey.
22 23 24 |
# File 'lib/janis.rb', line 22 def clientkey @clientkey end |
.options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/janis.rb', line 22 def @options end |
.platform ⇒ Object
Returns the value of attribute platform.
22 23 24 |
# File 'lib/janis.rb', line 22 def platform @platform end |
.token ⇒ Object
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 |
.headers ⇒ Object
59 60 61 |
# File 'lib/janis.rb', line 59 def headers @headers = {'apikey':apikey,'clientkey':clientkey,'platform':platform, 'token': token} end |
.hooks ⇒ Object
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 |
.initialize ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/janis.rb', line 24 def initialize @apikey @clientkey @token @platform @options end |
.janisappid ⇒ Object
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) = x['message'] recipientid = x['recipient']['id'] appid = ['app_id'] is_echo = ['is_echo'] if ['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 |