-
Notifications
You must be signed in to change notification settings - Fork 2
AsyncRequestHandler Class
Innocent Bystander edited this page Aug 17, 2015
·
6 revisions
This is the method documentation for AsyncRequestHandler class, which is the base class used by sinks in bot version 2.7 and above. This class heavily resembles the original BaseBotRequestHandler class, as it was designed as a drop-in replacement.
Some familiarity with aiohttp.web is recommended
def addroutes(self, router):
router.add_route("POST", "/{convid}", self.adapter_do_POST)
router.add_route("POST", "/{convid}/", self.adapter_do_POST)- receives incoming POST request matching the specified pattern and routes it to
self.adapter_do_POST - override this class to provide more incoming request routes, such as supporting GET request, or special paths
- no equivalent method in
BaseBotRequestHandler
@asyncio.coroutine
def adapter_do_POST()- if overridden, decorate with
@asyncio.coroutine - processes incoming POST request
- pseudo-code:
- breaks request into:
-
path- the URL component following the domain name -
query_string- the URL component following the path -
content- string payload in the body of the request
-
- calls
AsyncRequestHandler.process_request- if the function returns a non-empty string sequence, it will be output to the client as html, otherwise text
OKwill be sent back as the response
- if the function returns a non-empty string sequence, it will be output to the client as html, otherwise text
- breaks request into:
- emulates the original behaviour of
BaseBotRequestHandler.do_POST()with the added capability of being able to send back a different response
@asyncio.coroutine
def process_request( path,
query_string,
content )- if overridden, decorate with
@asyncio.coroutine - pseudo-code:
- receives
path,query_stringandcontent-
pathis split by "/" and the first element of the resulting list is assumed to be a targetconversation_id(to send a message) -
query_stringis ignored -
contentis assumed to be valid JSON and parsed intodict payload:-
payload["echo"]will be the html-formatted message to send -
payload["image"]can consist of two subkeys:-
payload["image"]["base64encoded"]is a base64-encoded string of an image to send, it is decoded into aio.BytesIOobjectimage_data -
payload["image"]["filename"]is the filename of the image and assigned toimage_filename- if no filename is provided, will attempt to determine image type and assign the correct extension with a name based on the current date and time
-
-
-
- calls
send_data(conversation_id, html, image_data=None, image_filename=None) - return any results from
send_data()
- receives
- emulates the original behaviour of
BaseBotRequestHandler.process_request(), with addedreturn
@asyncio.coroutine
def send_data( conversation_id,
html,
image_data=None,
image_filename=None,
context=None )- sends a message containing html-formatted text and/or an image to a conversation
- if overridden, decorate with
@asyncio.coroutine - pseudo-code:
- if
image_datais provided, the image will be automatically uploaded for use by the bot-
image_filenameis optional but recommended - sinceprocess_requestdetermines the type of image uploaded. if the method was overridden and noimage_filenameprovided, it will fallback to<datetime>.jpgand output a warning
-
-
htmlis parsed by the available parser in the bot - as of 2.4 the parser supports simplified Markdown and HTML - sends the message to the appropriate conversation
- returns "OK"
- if
- emulates the original behaviour of
BaseBotRequestHandler.send_data(), with extrareturn
Plugin List | Developer Reference: [ Intro | Plugins | Sinks | In-built Functionality | [Configuration] (Configuration) ]