Monday, 10 November 2014

Generating Text messages, INFO messages from OPENSIPS script

By vm  |  05:47 No comments

Opensips is a SIP Server. I thought it is not capable to generate sip messages from it's script file i.e opensips.cfg. because opensips is basically SIP server, Server always serves request (i.e give response ) to it's UAC and unable to generate request from it. Then i had a requirement i need to send Text Message to my User Agent Client. I actually started searching about it Finally i came to Know that  OPENSIPS is able to Send text messages from it's Script. Today in this Short tutorial we will discuss about "How to Generate Custom Messages from opensips script".


How to generate Text messages from OPENSIPS script :

This can be done by using t_uac_dlg exported MI function of TM module. and im_send.php file used to send Instant messages from OPENSIPS to any User agent client.

I am trying to generate Text message from Opensips.cfg and sending it to desired UAC.

Here is the Details about t_uac_dlg function

t_uac_dlg :

Generates and sends a local SIP request.

Parameters:

  • method - request method
  • RURI - request SIP URI
  • NEXT HOP - next hop SIP URI (OBP); use “.” if no value.
  • socket - local socket to be used for sending the request; use “.” if no value.
  • headers - set of additional headers to be added to the request; at least “From” and “To” headers must be specify)
  • body - (optional, may not be present) request body (if present, requires the “Content-Type” and “Content-length” headers)
we are also using im_send.php file. you can get this file from GITHUB OPENSIPS repositary.
my im_send.php file look like this..

im_send.php file for Sending Custom Text message :

  1. <?php
  2. /* config values */
  3. $web_contact="sip:daemon@mydomain.net";
  4. $fifo="/tmp/opensips_fifo";
  5. $signature="web_im_0.1.0";
  6.  
  7. /* open reply fifo */
  8. $myfilename="webfifo_".rand();
  9. $mypath="/tmp/".$myfilename;
  10. $outbound_proxy=".";
  11.  
  12.  
  13. // store argument 1 in to_user address
  14. if (isset($argv[1])) {
  15.         $sip_address = $argv[1];
  16. }
  17.  
  18. // store argument 2 in $fromNumber variable - this is From DID
  19. if (isset($argv[2])) {
  20.         $fromNumber=$argv[2];
  21. }
  22.  
  23. // store argument 3 is Error Message..
  24. if (isset($argv[3])) {
  25.         $instant_message = $argv[3];
  26. }
  27.  
  28. echo "Initiating your request...<p>";
  29.  
  30. /* open fifo now */
  31. $fifo_handle=fopen( $fifo, "w" );
  32. if (!$fifo_handle) {
  33.     exit ("Sorry -- cannot open fifo: ".$fifo);
  34. }
  35.  
  36. /* construct FIFO command */
  37. $fifo_cmd=":t_uac_dlg:".$myfilename."\n".
  38.     "MESSAGE\n".
  39.     $sip_address."\n".
  40.         $outbound_proxy."\n".
  41.     ".\n".
  42.     "\"From: ".$fromNumber."\r\n".
  43.         "To: ".$sip_address."\r\n".
  44.         "p-version: ".$signature."\r\n".
  45.     "Contact: ".$fromNumber."\r\n".
  46.     "Content-Type: text/plain; charset=UTF-8\r\n".
  47.     "\"\n".
  48.     "\"".$instant_message."\"".
  49.         "\n\n";
  50.  
  51. /* create fifo for replies */
  52. system("mkfifo -m 666 ".$mypath );
  53.  
  54. /* write fifo command */
  55. if (fwrite( $fifo_handle, $fifo_cmd)==-1) {
  56.     unlink($mypath);
  57.     fclose($fifo_handle);
  58.     exit("Sorry -- fifo writing error");
  59. }
  60. fclose($fifo_handle);
  61.  
  62. /* read output now */
  63. if (readfile( $mypath )==-1) {
  64.     unlink($mypath);
  65.         exit("Sorry -- fifo reading error");
  66. }
  67. unlink($mypath);
  68. echo "<p>Thank you for using IM<p>";
  69.  
  70. ?>

This im_send.php takes three arguments.. 
      1) to_user
      2) form _user -==> Here opensips generating Message so.. opensips itself is From user..
      3) Custom Message 
 ex :  Hello UAC this message Genetated from opensips.cfg

Save this file as *.php and now we have im_send.php file. Need to call this file from opensips script

Calling im_send.php file from opensips Script i.e opensips.cfg:

loadmodule "uac.so"
loadmodule "exec.so"

call above im_send.php file whenever you want to send text message. like this

$avp(message)="Hello UAC this is Custom Text Message";
exec_avp("php /usr/local/sbin/im_send.php '$fu' '$ru' '$avp(message)'","$avp(status)");

Make sure the path of im_send.php file is correct. also check file permissions Opensips running user need to have permissions to access that file.
i recommend 755 for that file.

Congratulations, You're Successfully Configured opensips to generate Custom Messages.



Author: vm

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.

0 comments:

E-mail Newsletter

Sign up now to receive breaking news and to hear what's new with us.

Recent Articles

© 2014 VOIP4Learn. WP themonic converted by Bloggertheme9. Powered by Blogger.
TOP