englercj / node-esl

FreeSWITCH ESL implementation for Node.js; implements the full Event Socket Library specified in: http://wiki.freeswitch.org/wiki/Esl
http://englercj.github.com/node-esl/
MIT License
170 stars 111 forks source link

Example for usage of sendEvent #81

Closed ednt closed 5 years ago

ednt commented 5 years ago

Hi,

we use V1.2.1. and get: [error]: event.getHeader is not a function when we try to use

const resyncExt = (domainName, extNumber, callback) => {
    let eventCommand = "Event-Name: NOTIFY\n";
    eventCommand += "profile: internal\n";
    eventCommand += "content-type: application/simple-message-summary\n";
    eventCommand += "event-string: check-sync;reboot=false\n";
    eventCommand += "user: " + extNumber + "\n";
    eventCommand += "host: " + domainName + "\n";

    freeSwitchConn.sendEvent(eventCommand, (res) => {
        if(res.body.substr(0,1) === "+")
            callback(true);
        else
            callback(false);
    });
};

We found no example how to use it. So the above is only a guess. Can you tell us how to use it right?

Best regards

englercj commented 5 years ago

sendEvent takes an Event object, not a string.

ednt commented 5 years ago

Hi, thanks for your response, but ...

we run still into problems. First it is not easy to build an object with a dash inside the key. We tried now

data["Event-Name"] = "NOTIFY";

Second we still get the error event.getHeader is not a function

We would really appreciate it, if you can show us a working example.

englercj commented 5 years ago

First it is not easy to build an object with a dash inside the key.

Sure it is, JS supports it natively.

Second we still get the error event.getHeader is not a function

Probably because you aren't creating an Event object.

Example:

const eventCommand = new esl.Event({
    'Event-Name': 'NOTIFY',
    'profile': 'internal',
    'event-string': 'check-sync;reboot=false',
    'user': extNumber.
    'host': domainName,
});

freeSwitchConn.sendEvent(eventCommand, (res) => {
    if(res.body.substr(0,1) === "+")
        callback(true);
    else
        callback(false);
});
ednt commented 5 years ago

Thank you for your help!

The full working example:

const eventCommand = new esl.Event({
                            "Event-Name": "NOTIFY",
                            "profile": "internal",
                            "event-string": "check-sync;reboot=false",
                            "content-type": "application/simple-message-summary",
                            "user": ext_number,
                            "host": tokenData.domainName
                        });

const resyncExt = (eventCommand, callback) => {

    let success = false;

    freeSwitchConn.sendEvent(eventCommand, (res) => {
        for(let obj of res.headers){
            if(obj.name === "Reply-Text")
                if(obj.value.substr(0,1) === "+"){
                    success = true;
                    break;
                }
        }
        if(success)
            callback(true);
        else
            callback(false);
    });
};
englercj commented 5 years ago

You can use getHeader("Reply-Text") instead of looping through all headers. In v2, getHeaders is faster than looping as well.