def handleMessage(self):
        try:
            eprint(self.data);
            tag = self.data.split(",", 1)[0];
            obj = json.loads(self.data[len(tag)+1:]);

            eprint(obj);
            if "from" not in obj or obj["from"] != "api2backend" or "type" not in obj or not (("command" in obj and obj["command"] == "backend-connectWhatsApp") or "whatsapp_instance_id" in obj):
                self.sendError("Invalid request");
                return;

            if obj["type"] == "call":
                if "command" not in obj:
                    self.sendError("Invalid request");
                    return;

                if obj["command"] == "backend-connectWhatsApp":
                    clientInstanceId = uuid.uuid4().hex;
                    onOpenCallback = {
                        "func": lambda cbSelf: self.sendJSON(mergeDicts({ "type": "resource_connected", "resource": "whatsapp" }, getAttr(cbSelf, "args")), getAttr(cbSelf, "tag")),
                        "tag": tag,
                        "args": { "resource_instance_id": clientInstanceId }
                    };
                    onMessageCallback = {
                        "func": lambda obj, cbSelf, moreArgs=None: self.sendJSON(mergeDicts(mergeDicts({ "type": "whatsapp_message_received", "message": obj, "timestamp": getTimestampMs() }, getAttr(cbSelf, "args")), moreArgs), getAttr(cbSelf, "tag")),
                        "args": { "resource_instance_id": clientInstanceId }
                    };
                    onCloseCallback = {
                        "func": lambda cbSelf: self.sendJSON(mergeDicts({ "type": "resource_gone", "resource": "whatsapp" }, getAttr(cbSelf, "args")), getAttr(cbSelf, "tag")),
                        "args": { "resource_instance_id": clientInstanceId }
                    };
                    self.clientInstances[clientInstanceId] = WhatsAppWebClient(onOpenCallback, onMessageCallback, onCloseCallback);
                else:
                    currWhatsAppInstance = self.clientInstances[obj["whatsapp_instance_id"]];
                    callback = {
                        "func": lambda obj, cbSelf: self.sendJSON(mergeDicts(obj, getAttr(cbSelf, "args")), getAttr(cbSelf, "tag")),
                        "tag": tag,
                        "args": { "resource_instance_id": obj["whatsapp_instance_id"] }
                    };
                    if currWhatsAppInstance.activeWs is None:
                        self.sendError("No WhatsApp server connected to backend.");
                        return;

                    cmd = obj["command"];
                    if cmd == "backend-generateQRCode":
                        currWhatsAppInstance.generateQRCode(callback);
                    elif cmd == "backend-getLoginInfo":
                        currWhatsAppInstance.getLoginInfo(callback);
                    elif cmd == "backend-getConnectionInfo":
                        currWhatsAppInstance.getConnectionInfo(callback);
                    elif cmd == "backend-disconnectWhatsApp":
                        currWhatsAppInstance.disconnect();
                        self.sendJSON({ "type": "resource_disconnected", "resource": "whatsapp", "resource_instance_id": obj["whatsapp_instance_id"] }, tag);
        except:
            eprint(traceback.format_exc());
Exemplo n.º 2
0
    def handleMessage(self):
        try:
            eprint("handle data:" + self.data)
            tag = self.data.split(",", 1)[0]
            obj = json.loads(self.data[len(tag) + 1:])

            if "from" not in obj or obj["from"] != "api2backend" or \
                "type" not in obj or not \
                    (("command" in obj and
                        obj["command"] == "backend-connectWhatsApp") or
                        "whatsapp_instance_id" in obj):
                self.sendError("Invalid request")
                return

            if obj["type"] == "call":
                if "command" not in obj:
                    self.sendError("Invalid request")
                    return

                if obj["command"] == "backend-connectWhatsApp":
                    clientInstanceId = uuid.uuid4().hex
                    onOpenCallback = {
                        "func":
                        lambda cbSelf: self.sendJSON(
                            mergeDicts(
                                {
                                    "type": "resource_connected",
                                    "resource": "whatsapp"
                                }, getAttr(cbSelf, "args")),
                            getAttr(cbSelf, "tag")),
                        "tag":
                        tag,
                        "args": {
                            "resource_instance_id": clientInstanceId
                        }
                    }
                    onMessageCallback = {
                        "func":
                        lambda obj, cbSelf, moreArgs=None: self.sendJSON(
                            mergeDicts(
                                mergeDicts(
                                    {
                                        "type": "whatsapp_message_received",
                                        "message": obj,
                                        "timestamp": getTimestampMs()
                                    }, getAttr(cbSelf, "args")), moreArgs),
                            getAttr(cbSelf, "tag")),
                        "args": {
                            "resource_instance_id": clientInstanceId
                        }
                    }
                    onCloseCallback = {
                        "func":
                        lambda cbSelf: self.sendJSON(
                            mergeDicts(
                                {
                                    "type": "resource_gone",
                                    "resource": "whatsapp"
                                }, getAttr(cbSelf, "args")),
                            getAttr(cbSelf, "tag")),
                        "args": {
                            "resource_instance_id": clientInstanceId
                        }
                    }
                    self.clientInstances[clientInstanceId] = WhatsAppWebClient(
                        onOpenCallback, onMessageCallback, onCloseCallback)
                else:
                    currWhatsAppInstance = self.clientInstances[
                        obj["whatsapp_instance_id"]]
                    callback = {
                        "func":
                        lambda obj, cbSelf: self.sendJSON(
                            mergeDicts(obj, getAttr(cbSelf, "args")),
                            getAttr(cbSelf, "tag")),
                        "tag":
                        tag,
                        "args": {
                            "resource_instance_id": obj["whatsapp_instance_id"]
                        }
                    }
                    if currWhatsAppInstance.activeWs is None:
                        errorMsg = "No WhatsApp server connected to backend."
                        self.sendError(errorMsg)
                        return

                    cmd = obj["command"]
                    if cmd == "backend-generateQRCode":
                        currWhatsAppInstance.generateQRCode(callback)
                    elif cmd == "backend-getLoginInfo":
                        currWhatsAppInstance.getLoginInfo(callback)
                    elif cmd == "backend-getConnectionInfo":
                        currWhatsAppInstance.getConnectionInfo(callback)
                    elif cmd == "backend-SendWhatsAppMessage":
                        payload = obj["payload"]
                        eprint("Mensagem enviada")
                        currWhatsAppInstance.sendTextMessage(
                            payload["id"], payload["msg"])
                    elif cmd == "backend-disconnectWhatsApp":
                        currWhatsAppInstance.disconnect()
                        self.sendJSON(
                            {
                                "type": "resource_disconnected",
                                "resource": "whatsapp",
                                "resource_instance_id":
                                obj["whatsapp_instance_id"]
                            }, tag)

        except:
            eprint("An IndexError or KeyError occurred!")
            eprint(traceback.format_exc())
Exemplo n.º 3
0
    def handleMessage(self):
        try:
            tag = self.data.split(",", 1)[0]
            obj = json.loads(self.data[len(tag) + 1:])

            if "from" not in obj or obj[
                    "from"] != "api2backend" or "type" not in obj or not (
                        ("command" in obj
                         and obj["command"] == "backend-connectWhatsApp")
                        or "whatsapp_instance_id" in obj):
                self.sendError("Invalid request")
                return

            if obj["type"] == "call":
                if "command" not in obj:
                    self.sendError("Invalid request")
                    return

                if obj["command"] == "backend-connectWhatsApp":
                    clientInstanceId = uuid.uuid4().hex
                    onOpenCallback = {
                        "func":
                        lambda cbSelf: self.sendJSON(
                            mergeDicts(
                                {
                                    "type": "resource_connected",
                                    "resource": "whatsapp"
                                }, getAttr(cbSelf, "args")),
                            getAttr(cbSelf, "tag")),
                        "tag":
                        tag,
                        "args": {
                            "resource_instance_id": clientInstanceId
                        }
                    }
                    onMessageCallback = {
                        "func":
                        lambda obj, cbSelf, moreArgs=None: self.nop(obj),
                        "args": {
                            "resource_instance_id": clientInstanceId
                        }
                    }
                    onCloseCallback = {
                        "func":
                        lambda cbSelf: self.sendJSON(
                            mergeDicts(
                                {
                                    "type": "resource_gone",
                                    "resource": "whatsapp"
                                }, getAttr(cbSelf, "args")),
                            getAttr(cbSelf, "tag")),
                        "args": {
                            "resource_instance_id": clientInstanceId
                        }
                    }
                    self.clientInstances[clientInstanceId] = WhatsAppWebClient(
                        onOpenCallback, onMessageCallback, onCloseCallback)
                else:
                    currWhatsAppInstance = self.clientInstances[
                        obj["whatsapp_instance_id"]]
                    callback = {
                        "func":
                        lambda obj, cbSelf: self.sendJSON(
                            mergeDicts(obj, getAttr(cbSelf, "args")),
                            getAttr(cbSelf, "tag")),
                        "tag":
                        tag,
                        "args": {
                            "resource_instance_id": obj["whatsapp_instance_id"]
                        }
                    }
                    if currWhatsAppInstance.activeWs is None:
                        self.sendError(
                            "No WhatsApp server connected to backend.")
                        return

                    cmd = obj["command"]
                    if cmd == "backend-generateQRCode":
                        currWhatsAppInstance.generateQRCode(callback)
                    elif cmd == "backend-regenerateQRCode":
                        currWhatsAppInstance.regenerateQRCode(callback)
                    elif cmd == "backend-getPhoto":
                        currWhatsAppInstance.getPhoto(obj["phone"], callback)
                    elif cmd == "backend-getLoginInfo":
                        currWhatsAppInstance.getLoginInfo(callback)
                    elif cmd == "backend-getConnectionInfo":
                        currWhatsAppInstance.getConnectionInfo(callback)
                    elif cmd == "backend-disconnectWhatsApp":
                        currWhatsAppInstance.disconnect()
                        self.sendJSON(
                            {
                                "type": "resource_disconnected",
                                "resource": "whatsapp",
                                "resource_instance_id":
                                obj["whatsapp_instance_id"]
                            }, tag)
        except:
            log(2, 'an error while processing message', traceback.format_exc())