pep.py/handlers/apiFokabotMessageHandler.py

48 lines
1.2 KiB
Python
Raw Normal View History

import json
2016-10-02 20:48:14 +00:00
import tornado.web
import tornado.gen
from common.sentry import sentry
2016-10-02 20:48:14 +00:00
from common.web import requestsManager
from constants import exceptions
from helpers import chatHelper
2016-10-02 20:48:14 +00:00
from objects import glob
2016-10-02 20:48:14 +00:00
class handler(requestsManager.asyncRequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
@sentry.captureTornado
def asyncGet(self):
statusCode = 400
data = {"message": "unknown error"}
try:
# Check arguments
2016-10-02 20:48:14 +00:00
if not requestsManager.checkArguments(self.request.arguments, ["k", "to", "msg"]):
raise exceptions.invalidArgumentsException()
# Check ci key
key = self.get_argument("k")
if key is None or key != glob.conf.config["server"]["cikey"]:
raise exceptions.invalidArgumentsException()
chatHelper.sendMessage(
glob.BOT_NAME,
self.get_argument("to").encode().decode("ASCII", "ignore"),
self.get_argument("msg").encode().decode("ASCII", "ignore")
)
# Status code and message
statusCode = 200
data["message"] = "ok"
except exceptions.invalidArgumentsException:
statusCode = 400
data["message"] = "invalid parameters"
finally:
# Add status code to data
data["status"] = statusCode
# Send response
self.write(json.dumps(data))
self.set_status(statusCode)