Now Hiring: Are you a driven and motivated 1st Line IT Support Engineer?

WebHook Example Script

Create a script with the following content and provide its URL as WebHook. #

 

  • </pre>
    <pre class="prettyprint prettyprinted"><span class="pln">define</span><span class="pun">(</span><span class="str">"API_KEY"</span><span class="pun">,</span> <span class="str">"b7d52620813455b38a3a0d5bc801dfcc9946f34f"</span><span class="pun">);</span>
    
    <span class="kwd">try</span> <span class="pun">{</span>
        <span class="kwd">if</span> <span class="pun">(</span><span class="pln">isset</span><span class="pun">(</span><span class="pln">$_SERVER</span><span class="pun">[</span><span class="str">"HTTP_X_SG_SIGNATURE"</span><span class="pun">]))</span> <span class="pun">{</span>
            <span class="kwd">if</span> <span class="pun">(</span><span class="pln">isset</span><span class="pun">(</span><span class="pln">$_POST</span><span class="pun">[</span><span class="str">"messages"</span><span class="pun">]))</span> <span class="pun">{</span><span class="pln">
                $hash </span><span class="pun">=</span><span class="pln"> base64_encode</span><span class="pun">(</span><span class="pln">hash_hmac</span><span class="pun">(</span><span class="str">'sha256'</span><span class="pun">,</span><span class="pln"> $_POST</span><span class="pun">[</span><span class="str">"messages"</span><span class="pun">],</span><span class="pln"> API_KEY</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">));</span>
                <span class="kwd">if</span> <span class="pun">(</span><span class="pln">$hash </span><span class="pun">===</span><span class="pln"> $_SERVER</span><span class="pun">[</span><span class="str">"HTTP_X_SG_SIGNATURE"</span><span class="pun">])</span> <span class="pun">{</span><span class="pln">
                    $messages </span><span class="pun">=</span><span class="pln"> json_decode</span><span class="pun">(</span><span class="pln">$_POST</span><span class="pun">[</span><span class="str">"messages"</span><span class="pun">],</span> <span class="kwd">true</span><span class="pun">);</span>
        
                    <span class="com">/**
                     * For example :-
                     * $messages = [
                     *                 0 => [
                     *                          "ID" => "1",
                     *                          "number" => "+11234567890",
                     *                          "message" => "This is a test message.",
                     *                          "deviceID" => "1",
                     *                          "simSlot" => "0",
                     *                          "userID" => "1",
                     *                          "status" => "Received",
                     *                          "sentDate" => "2018-10-20T00:00:00+02:00",
                     *                          "deliveredDate" => "2018-10-20T00:00:00+02:00"
                     *                          "groupID" => null
                     *                      ]
                     *             ]
                     *
                     * senDate represents the date and time when the message was received on the device.
                     * deliveredDate represents the date and time when the message was received by the server.
                     */</span>
        
                    <span class="kwd">foreach</span> <span class="pun">(</span><span class="pln">$messages </span><span class="kwd">as</span><span class="pln"> $message</span><span class="pun">)</span> <span class="pun">{</span>
                        <span class="kwd">if</span><span class="pun">(</span><span class="pln">strtolower</span><span class="pun">(</span><span class="pln">$message</span><span class="pun">[</span><span class="str">"message"</span><span class="pun">])</span> <span class="pun">===</span> <span class="str">"hi"</span><span class="pun">)</span> <span class="pun">{</span>
                            <span class="com">// Reply to message using API or execute some commands. Possibilities are limitless.</span>
                        <span class="pun">}</span>
                    <span class="pun">}</span>
                <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
                    <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">Exception</span><span class="pun">(</span><span class="str">"Signature don't match!"</span><span class="pun">);</span>
                <span class="pun">}</span>
            <span class="pun">}</span> <span class="kwd">else</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">isset</span><span class="pun">(</span><span class="pln">$_POST</span><span class="pun">[</span><span class="str">"ussdRequest"</span><span class="pun">]))</span> <span class="pun">{</span><span class="pln">
                $hash </span><span class="pun">=</span><span class="pln"> base64_encode</span><span class="pun">(</span><span class="pln">hash_hmac</span><span class="pun">(</span><span class="str">'sha256'</span><span class="pun">,</span><span class="pln"> $_POST</span><span class="pun">[</span><span class="str">"ussdRequest"</span><span class="pun">],</span><span class="pln"> API_KEY</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">));</span>
                <span class="kwd">if</span> <span class="pun">(</span><span class="pln">$hash </span><span class="pun">===</span><span class="pln"> $_SERVER</span><span class="pun">[</span><span class="str">"HTTP_X_SG_SIGNATURE"</span><span class="pun">])</span> <span class="pun">{</span><span class="pln">
                    $ussdRequest </span><span class="pun">=</span><span class="pln"> json_decode</span><span class="pun">(</span><span class="pln">$_POST</span><span class="pun">[</span><span class="str">"ussdRequest"</span><span class="pun">]);</span><span class="pln">
                    $deviceID </span><span class="pun">=</span><span class="pln"> $ussdRequest</span><span class="pun">-></span><span class="pln">deviceID</span><span class="pun">;</span><span class="pln">
                    $simSlot </span><span class="pun">=</span><span class="pln"> $ussdRequest</span><span class="pun">-></span><span class="pln">simSlot</span><span class="pun">;</span><span class="pln">
                    $request </span><span class="pun">=</span><span class="pln"> $ussdRequest</span><span class="pun">-></span><span class="pln">request</span><span class="pun">;</span><span class="pln">
                    $response </span><span class="pun">=</span><span class="pln"> $ussdRequest</span><span class="pun">-></span><span class="pln">response</span><span class="pun">;</span>
                    
                    <span class="com">// Do whatever you want with data you received.</span>
                <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
                    <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">Exception</span><span class="pun">(</span><span class="str">"Signature don't match!"</span><span class="pun">);</span>
                <span class="pun">}</span>
            <span class="pun">}</span>
        <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span><span class="pln">
            http_response_code</span><span class="pun">(</span><span class="lit">400</span><span class="pun">);</span><span class="pln">
            error_log</span><span class="pun">(</span><span class="str">"Signature not found!"</span><span class="pun">);</span>
        <span class="pun">}</span>
    <span class="pun">}</span> <span class="kwd">catch</span> <span class="pun">(</span><span class="typ">Exception</span><span class="pln"> $e</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
        http_response_code</span><span class="pun">(</span><span class="lit">401</span><span class="pun">);</span><span class="pln">
        error_log</span><span class="pun">(</span><span class="pln">$e</span><span class="pun">-></span><span class="pln">getMessage</span><span class="pun">());</span>
    <span class="pun">}</span></pre>
    <pre>

Leave your thought here

Your email address will not be published. Required fields are marked *