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".
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
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 :
- <?php
- /* config values */
- $web_contact="sip:daemon@mydomain.net";
- $fifo="/tmp/opensips_fifo";
- $signature="web_im_0.1.0";
- /* open reply fifo */
- $myfilename="webfifo_".rand();
- $mypath="/tmp/".$myfilename;
- $outbound_proxy=".";
- // store argument 1 in to_user address
- if (isset($argv[1])) {
- $sip_address = $argv[1];
- }
- // store argument 2 in $fromNumber variable - this is From DID
- if (isset($argv[2])) {
- $fromNumber=$argv[2];
- }
- // store argument 3 is Error Message..
- if (isset($argv[3])) {
- $instant_message = $argv[3];
- }
- echo "Initiating your request...<p>";
- /* open fifo now */
- $fifo_handle=fopen( $fifo, "w" );
- if (!$fifo_handle) {
- exit ("Sorry -- cannot open fifo: ".$fifo);
- }
- /* construct FIFO command */
- $fifo_cmd=":t_uac_dlg:".$myfilename."\n".
- "MESSAGE\n".
- $sip_address."\n".
- $outbound_proxy."\n".
- ".\n".
- "\"From: ".$fromNumber."\r\n".
- "To: ".$sip_address."\r\n".
- "p-version: ".$signature."\r\n".
- "Contact: ".$fromNumber."\r\n".
- "Content-Type: text/plain; charset=UTF-8\r\n".
- "\"\n".
- "\"".$instant_message."\"".
- "\n\n";
- /* create fifo for replies */
- system("mkfifo -m 666 ".$mypath );
- /* write fifo command */
- if (fwrite( $fifo_handle, $fifo_cmd)==-1) {
- unlink($mypath);
- fclose($fifo_handle);
- exit("Sorry -- fifo writing error");
- }
- fclose($fifo_handle);
- /* read output now */
- if (readfile( $mypath )==-1) {
- unlink($mypath);
- exit("Sorry -- fifo reading error");
- }
- unlink($mypath);
- echo "<p>Thank you for using IM<p>";
- ?>
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.
0 comments: