{"id":144,"date":"2018-02-22T10:37:03","date_gmt":"2018-02-22T15:37:03","guid":{"rendered":"http:\/\/blog.voipxswitch.com\/?p=144"},"modified":"2019-08-30T14:05:11","modified_gmt":"2019-08-30T18:05:11","slug":"kamailio-controlling-presence-with-new-module-pua_json","status":"publish","type":"post","link":"https:\/\/blog.voipxswitch.com\/?p=144","title":{"rendered":"kamailio: controlling presence with new module `pua_json`"},"content":{"rendered":"<p>I recently submitted a new module to the <em>kamailio<\/em> project which adds an interface to update\/control presence records using json. Effectively, you can turn on\/off a phone&#8217;s Busy Lamp Field (BLF) by sending an http request containing json body to <em>kamailio<\/em>. The following is a quick guide on how to set up <em>kamailio<\/em> to accept http requests and update presence using two modules: <a href=\"http:\/\/kamailio.org\/docs\/modules\/devel\/modules\/xhttp\"><em>xhttp<\/em><\/a> and <a href=\"http:\/\/kamailio.org\/docs\/modules\/devel\/modules\/pua_json\"><em>pua_json<\/em><\/a>.<\/p>\n<h6>xhttp module<\/h6>\n<p>To setup <em>kamailio<\/em> to listen for http requests, you need to listen on a tcp interface.\u00a0 I&#8217;ve setup <em>kamailio<\/em> to listen on <em>localhost<\/em> because I don&#8217;t need to expose this service outside of the local machine. So in my <em>kamailio.cfg<\/em>, I have:<\/p>\n<pre># allow http requests\nlisten=tcp:127.0.0.1:8080\ntcp_accept_no_cl=yes\n\n# load the module\nloadmodule \"xhttp.so<\/pre>\n<h6>pua_json module<\/h6>\n<p>The <em>pua_json<\/em> requires no configuration parameters at this time. It simply exposes a function to update presentity from a json object. You will probably want the <em>json<\/em> module loaded so you can parse and validate some of the json. And you will need to properly configure all your presence modules to handle SIP SUBSCRIBEs, etc&#8230;<\/p>\n<pre># load modules\nloadmodule \"json.so\"\nloadmodule \"pua_json.so\"<\/pre>\n<h6>the event route<\/h6>\n<p>The last piece of the setup is the xhttp event_route which is used to handle the http requests containing json object. This is a simple example of such a route:<\/p>\n<pre>event_route[xhttp:request] {\n    if ($hu != \"\/presence\/\") {\n        xlog(\"L_INFO\", \"invalid request [$hu]\");\n        xhttp_reply(\"404\", \"Not Found\", \"text\/html\", \"\");\n\u00a0       return;\n     }\n     $var(call-id) = $(rb{json.parse,Call-ID});\n     xlog(\"L_INFO\", \"$var(call-id)|log|[$hu] payload $rb\");\n     if ($(rb{json.parse,Event-Package}) == \"dialog\") {\n        xlog(\"L_INFO\", \"$var(call-id)|log|received $(rb{json.parse,Event-Package}) update for $(rb{json.parse,From}) $rb\");\n        pua_json_publish($rb);\n    }\n\u00a0   xhttp_reply(\"200\", \"OK\", \"text\/html\", \"$rb\");\n}<\/pre>\n<p>The route basically checks to make sure the http endpoint matches `\/presence\/` and only executes presence updates for &#8220;dialog&#8221; events. I showed an example of how to parse the Call-ID using the json module as well.<\/p>\n<h6>the curl request<\/h6>\n<p>Once you have <em>kamailio<\/em> configured, you are ready to send a curl request to test presence!<br \/>\nTurn on a hold light with a request like so:<\/p>\n<blockquote>\n<pre>curl -d '{\"Call-ID\":\"park+01@test03.vxs.local\",\"Event-Category\":\"presence\",\"Event-Name\":\"update\",\"Event-Package\":\"dialog\",\"Expires\":\"3600\",\"To\":\"sip:user01@test03.vxs.local\",\"To-User\":\"user01\",\"To-Realm\":\"test03.vxs.local\",\"From\":\"sip:park+01@test03.vxs.local\",\"From-User\":\"park+01\",\"From-Realm\":\"test03.vxs.local\",\"State\":\"confirmed\"}' http:\/\/localhost:8080\/presence\/<\/pre>\n<\/blockquote>\n<p>Turn off a hold light with a request like (the only difference from above, is the &#8220;state&#8221; in this request):<\/p>\n<blockquote>\n<pre>curl -d '{\"Call-ID\":\"park+01@test03.vxs.local\",\"Event-Category\":\"presence\",\"Event-Name\":\"update\",\"Event-Package\":\"dialog\",\"Expires\":\"3600\",\"To\":\"sip:user01@test03.vxs.local\",\"To-User\":\"user01\",\"To-Realm\":\"test03.vxs.local\",\"From\":\"sip:park+01@test03.vxs.local\",\"From-User\":\"park+01\",\"From-Realm\":\"test03.vxs.local\",\"State\":\"terminated\"}' http:\/\/localhost:8080\/presence\/<\/pre>\n<\/blockquote>\n<p>If you have a phone subscribed to &#8220;park+01@test03.vxs.local&#8221; you should see the BLF color change when the state is confirmed, and then change back to the original color after the 2nd curl request.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently submitted a new module to the kamailio project which adds an interface to update\/control presence records using json. Effectively, you can turn on\/off a phone&#8217;s Busy Lamp Field (BLF) by sending an http request containing json body to kamailio. The following is a quick guide on how to set up kamailio to accept [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,25,15],"tags":[],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-kamailio","category-sip","category-voip"],"_links":{"self":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=144"}],"version-history":[{"count":11,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":159,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions\/159"}],"wp:attachment":[{"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.voipxswitch.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}