Montesuma80 / 3cx-web-API

an local API to Controll your 3CX v16/v18 over HTTP
76 stars 38 forks source link

Webhook #24

Open GarethWright opened 1 year ago

GarethWright commented 1 year ago

How easy is it to use this to implement a webhook on an incoming call? I need to monitor a queue for calls and process background jobs for particular numbers.

m4aax16 commented 1 year ago

@GarethWright It would be nice if this package would be able to manage it. I have the following solution :

  1. Create the file "start.vbs" on your Windows Desktop PC where 3CX Windows client is installed.
  2. Replace [YOUR_WEBHOOK_URL] and [YOUR_SECRET]. YOUR_WEBHOOK_URL is the URL with your Webhook Endpoint like https://example.com/webhook/phonecall YOUR_SECRET is any random secret.
Set WshShell = CreateObject("WScript.Shell")
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

' Get the command-line arguments
' '{"callerNumber":"%CallerNumber%","callerDisplayName":"%CallerDisplayName%","agentId":"100"}'
Set args = WScript.Arguments

' Check if one command-line argument is provided
If args.Count <> 1 Then
    WScript.Echo "Invalid number of arguments. Usage: cscript 3cxextension.vbs <jsonData>"
Else
    ' Extract the JSON data from the command-line argument
    jsonData = args.Item(0)

    ' Construct the request body JSON string
    requestBodyJson = "{""jsonData"": """ & Replace(jsonData, """", """""") & """}"

    ' Compute a simple signature
    secret = "[YOUR_SECRET]"
    signature = requestBodyJson & secret

    ' Make the HTTP POST request
    url = "[YOUR_WEBHOOK_URL]"
    xmlhttp.Open "POST", url, False
    xmlhttp.setRequestHeader "Content-Type", "application/json"
    xmlhttp.setRequestHeader "Signature", signature
    xmlhttp.send requestBodyJson

    ' Get the response
    responseStatusCode = xmlhttp.Status

    ' Handle the response
    If responseStatusCode = 200 Then
        ' Request succeeded
        ' WScript.Echo "HTTP POST request succeeded!"
    Else
        ' Request failed
        WScript.Echo "HTTP POST request failed with status code: " & responseStatusCode
    End If
End If

Set xmlhttp = Nothing
Set WshShell = Nothing
  1. Open your 3CX Windows Phone
  2. Navigate to Settings -> Advanced -> Behave
  3. in the "Extern Application" section , enable "Launch application on incoming call"
  4. Select the start.vbs file at "Path to Application"
  5. Set at Parameters to send '{"callerNumber":"%CallerNumber%","callerDisplayName":"%CallerDisplayName%","agentId":"100"}'
  6. Set notify when "Connected"

Caution : agentID must be entered here manually for each client

How to decode the Webhook data How I do it with Laravel :

class PhoneCallRequestValidator implements Validator
{
    public function isValid(Request $request, WebhookConfig $config): bool
    {
        $computedSignature = $request->getContent().env('WEBHOOK_CLIENT_SECRET');

        return hash_equals($computedSignature, $request->header('Signature'));
    }
}

Btw. it works also for outbound calls

On the 3CX Windows Client you are able to enable automatically accept the calls here :

Settings -> advanced -> automatic call acceptance

GarethWright commented 1 year ago

That’s great. I actually ended up using a CFD.

In short we have intercoms on a product and I wanted to notify our VMS system to pop up the cameras on our monitoring station when the intercom is pressed.

CFD answers the call and fetches the callerID and uses that to create a fake email address.

I then made a basic smtp server which takes an email sent by CFD and passes the body (json) to our service via REST api before transferring the call to the main queue. It’s instant and we have cameras up automatically before the phone is off the hook!

Completely server side too.

On Thu, 29 Jun 2023 at 19:19, m4aax16 @.***> wrote:

@GarethWright https://github.com/GarethWright It would be nice if this package would be able to manage it. I have the following solution :

  1. Create the file "start.vbs"
  2. Replace [YOUR_WEBHOOK_URL] and [YOUR_SECRET]. YOUR_WEBHOOK_URL is the URL with your Webhook Endpoint like https://example.com/webhook/phonecall YOUR_SECRET is any random secret.

Set WshShell = CreateObject("WScript.Shell") Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

' Get the command-line arguments ' '{"callerNumber":"%CallerNumber%","callerDisplayName":"%CallerDisplayName%","agentId":"100"}' Set args = WScript.Arguments

' Check if one command-line argument is provided If args.Count <> 1 Then WScript.Echo "Invalid number of arguments. Usage: cscript 3cxextension.vbs " Else ' Extract the JSON data from the command-line argument jsonData = args.Item(0)

' Construct the request body JSON string
requestBodyJson = "{""jsonData"": """ & Replace(jsonData, """", """""") & """}"

' Compute a simple signature
secret = "[YOUR_SECRET]"
signature = requestBodyJson & secret

' Make the HTTP POST request
url = "[YOUR_WEBHOOK_URL]"
xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlhttp.setRequestHeader "Signature", signature
xmlhttp.send requestBodyJson

' Get the response
responseStatusCode = xmlhttp.Status

' Handle the response
If responseStatusCode = 200 Then
    ' Request succeeded
    ' WScript.Echo "HTTP POST request succeeded!"
Else
    ' Request failed
    WScript.Echo "HTTP POST request failed with status code: " & responseStatusCode
End If

End If

Set xmlhttp = Nothing Set WshShell = Nothing

  1. Open your 3CX Windows Phone
  2. Navigate to Settings -> Expanded -> Behave
  3. in the "Extern Application" section , enable "Launch application on incoming call"
  4. Select the start.vbs file at "Path to Application"
  5. Set at Parameters to send

    '{"callerNumber":"%CallerNumber%","callerDisplayName":"%CallerDisplayName%","agentId":"100"}'

  6. Set notify when "Connected"

Caution : agentID must be entered here manually for each client

How to decode the Webhook data How I do it with Laravel :

class PhoneCallRequestValidator implements Validator { public function isValid(Request $request, WebhookConfig $config): bool { $computedSignature = $request->getContent().env('WEBHOOK_CLIENT_SECRET');

    return hash_equals($computedSignature, $request->header('Signature'));
}

}

Btw. it works also for outbound calls

— Reply to this email directly, view it on GitHub https://github.com/Montesuma80/3cx-web-API/issues/24#issuecomment-1613582460, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACMMEYTXCLSMIRNQ4AA3W3XNW46PANCNFSM6AAAAAAZV7BTFU . You are receiving this because you were mentioned.Message ID: @.***>

-- Kind regards

Gareth Wright