module Janis

Constants

EVENTS

Attributes

apikey[RW]
clientkey[RW]
options[RW]
platform[RW]
token[RW]

Public Class Methods

assistanceRequested(x) click to toggle source
# File lib/janis.rb, line 121
def assistanceRequested(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/human', data)
end
headers() click to toggle source
# File lib/janis.rb, line 59
def headers
    @headers = {'apikey':apikey,'clientkey':clientkey,'platform':platform, 'token': token}
end
hooks() click to toggle source
# File lib/janis.rb, line 63
def hooks
    @hooks ||= {}
end
hopIn(x) click to toggle source
# File lib/janis.rb, line 106
def hopIn(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/in', data)
end
hopOut(x) click to toggle source
# File lib/janis.rb, line 111
def hopOut(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/out', data)
end
initialize() click to toggle source
# File lib/janis.rb, line 24
def initialize
    @apikey
    @clientkey
    @token
    @platform
    @options
end
janisappid() click to toggle source
# File lib/janis.rb, line 55
def janisappid
    @janisappid = 1242623579085955
end
logUnknownIntent(x) click to toggle source
# File lib/janis.rb, line 116
def logUnknownIntent(x)
    data = {'body':x, 'headers':headers}
    return Janiapi.post('/unknown', data)
end
new(*args, &block) click to toggle source
# File lib/janis.rb, line 32
def new(*args, &block)
    obj = allocate
    obj.initialize(*args, &block)
    obj
end
on(event, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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