📜 ⬆️ ⬇️

Click2Call on Mac OS X



Even before the Asterisk server appeared in the infrastructure of our organization, I regularly had a desire to make calls using a separate phone on the desk for numbers that I see on the monitor screen without dialing them on the device. Next, we will discuss the implementation of this functionality on a bunch of Mac OS X + Asterisk.


')
Readers may argue: connecting the headset to a computer and setting up the SIP client software solves the problem.

But for me personally there are several reasons to solve the problem differently:
  1. the habit of talking on the phone is still strong with the phone in hand (if necessary, you can turn on the speakerphone;
  2. there was an experience of using a wired and wireless headset, but it did not stick because of periodically arising technical discrepancies;
  3. A convenient feature can be redirection of a call to a SIP client of a mobile phone and then, for example, you can talk with your beloved not in the office with colleagues, but in the corridor or a cafe located behind the wall (even a bluetooth headset will not work in this scenario).


Theory


First of all, I tried to find an already existing Asterisk compatible Click2Call software, but none of the few projects found worked. Nevertheless, I got a general idea about the technology of interaction and being a programmer I decided to master the topic myself.

Components Used:


The process of initiating a call consists of the following steps.
  1. the local script generates the url and sends the request to the web server
  2. php on the web server connects to the Asterisk manager and makes a call
  3. Asterisk calls the SIP number of my phone and after answering it connects with the call of the remote subscriber


Since I was based on examples of setting up the possibility of ordering a callback through the site, there is an optional element in the scheme - a separate web server with PHP. It is also optional for the reason that in the current version of the script I have to use the PHP interpreter on the side of my Mac and, therefore, if necessary, you can transfer all the functionality of connecting to the Asterisk manager on a local machine. Nevertheless, I left a 3-component scheme for the possibility of initiating a call from a mobile phone browser, and not from a computer.

Practice


The process of setting up all three components will be presented in this order: Asterisk, WEB, Mac OS.

1. Configure Asterisk

First of all, you need to get Asterisk to call the local subscriber A and connect it with the subscriber B call. You can automate this process by placing the file with the required call parameters in the Asterisk server's / var / spool / asterisk / outgoing folder or transferring these parameters through the Asterisk process manager.

For example, a call file might be
Channel: Local/7777 MaxRetries: 1 RetryTime: 60 WaitTime: 30 Context: default Extension: +79201234567 Priority: 1 


It is important to correctly specify the Context parameter, which in the Asterisk configuration corresponds, for example, to the context for outgoing calls.

I conducted the testing in the first way - the file method, and set up the combat system using the second one. To do this, enable the Asterisk manager on the SIP server.

manager.conf
 [general] enabled = yes webenabled = yes port = 5038 [asterisk] secret=mysecretpassword deny=0.0.0.0 permit=ABC0 ;    read=system,call,log,verbose,command,agent,user,originate write=system,call,log,verbose,command,agent,user,originate 

Do not forget in the firewall to open access to port 5038 for the web server.

2. Cooking web

On the web server create a php script
 <?php $receiver = str_replace(array(" ","(", ")", "-", "."), "", $receiver); switch (strlen($receiver)) { case 0: exit; break; case 6: $receiver="84722".$receiver; break; case 10: $receiver="8".$receiver; break; } // IP   Asterisk $sys_ip = "1.2.3.4"; //     $User_str = "asterisk"; // ...    $Secret_str = "mysecretpassword"; $our_exten = "Local/$sender"; $WaitTime = "10"; $domain = "127.0.0.1"; //     sip-,    -  SIP/xxx $strCustdata = "Call to ".($name!=""?$name:$receiver)." <$receiver>"; $oSocket = fsockopen ($sys_ip, 5038, $errnum, $errdesc) or die ("Connection to host failed"); sleep (1); fputs ($oSocket, "Action: login\r\n"); fputs ($oSocket, "Username: $User_str\r\n"); fputs ($oSocket, "Secret: $Secret_str\r\n\r\n"); $wrets = fgets ($oSocket,128); echo $wrets; fputs ($oSocket, "Events: off\r\n\r\n"); fputs ($oSocket, "Action: originate\r\n"); fputs ($oSocket, "Channel: $our_exten\r\n"); fputs ($oSocket, "WaitTime: $WaitTime\r\n"); fputs ($oSocket, "CallerId: $strCustdata\r\n"); fputs ($oSocket, "Exten: $receiver\r\n"); fputs ($oSocket, "Context: default\r\n"); fputs ($oSocket, "Async: yes\r\n"); fputs ($oSocket, "Priority: 1\r\n\r\n"); fputs ($oSocket, "Action: Logoff\r\n\r\n"); sleep (2); fclose ($oSocket); ?> 

Important parameters in the formation of the call, as I said, are Channel, Exten and Context.

3. We prepare the initiator of calls

Create an Apple Script Editor and save it in the folder ~ / Library / Address Book Plug-Ins / script
 using terms from application "Contacts" on action property return "phone" end action property on action title for p with e return "Dial from Yealink" end action title on should enable action for p with e if value of e is missing value then return false else return true end if end should enable action on perform action for p with e set theName to name of p set telephone to the value of e tell application "Terminal" set param to "`echo \"<?php echo 'http://webserver/click2call/call.php?sender=7777&receiver='.urlencode('" & telephone & "').'&name='.urlencode('" & theName & "')?>\" | php`" do shell script "/opt/local/bin/wget -q -O - " & param & " >/dev/null 2>&1 & sleep 1" quit saving no end tell end perform action end using terms from 


The script generates the URL, accesses it via wget and then closes the terminal application. I had to use local PHP to encode UTF8 strings into URLs. All the functions found AppleScript could not cope with the task in full. If you keep Terminal.app open for work all the time, you may have to remove the command to close the application.

Now in the Contacts application in the context menu the item “Dial from Yealink” will appear. I note that after making changes to the script and saving it, you need to reload the Contacts application each time to test these edits.

To add an item to the Services context menu of any application, you need to create a service via Automator. Add a block Run Shell Script, enter the script itself
 /opt/local/bin/wget -q -O - "http://webserver/click2call/call.php?sender=7777&receiver=$1" > /dev/null 2>&1 

Set the input to be passed as an argument, not to standard input.


Total.


We get the opportunity to call from a landline SIP phone to the numbers that are visible on the screen. For example, this is how the context menu called in this article looks like:



Or like this:



Thanks for attention.

Source: https://habr.com/ru/post/170315/


All Articles