API Nummernabfrage


#!/usr/bin/php5 -q
<?php
require_once '/var/www/mobydickcmd/cmn/vendor/phphttpclient/httpful.phar';

$REST_API_KEY = 'your_rest_api_key';

/**
 * this AGI executs an REST request
 * @param $arg1 uri - URI, welche gerufen werden soll
 */
ob_implicit_flush( false );
set_time_limit( 10 );
error_reporting(0);
define ('__DEBUG__', true);

// create file handles if needed
if ( !defined( 'STDIN' ) )
    define( 'STDIN', fopen( 'php://stdin', 'r' ) );
if ( !defined( 'STDOUT' ) )
    define( 'STDOUT', fopen( 'php://stdout', 'w' ) );
if ( !defined( 'STDERR' ) )
    define( 'STDERR', fopen( 'php://stderr', 'w' ) );

// URI
if ( empty( $argv 1 ] ) ) {
    pushVariable( 'HASH(AGI_REST_PHONEBOOK,ERRNO)', 255 );
    pushVariable( 'HASH(AGI_REST_PHONEBOOK,ERRMSG)', 'no phonenumber' );
    fclose( STDIN );
    fclose( STDOUT );
    fclose( STDERR );
    exit( 1 );
}
$phonenumber = $argv 1 ];


/** test */
pushVariable('HTTP_CALLER_ID', 'hello world');
fclose( STDIN );
fclose( STDOUT );
fclose( STDERR );
exit( 0 );
***************


$uri = "http://tel.search.ch/api/?" . http_build_query(array('was' => $phonenumber, 'key' => $REST_API_KEY));
/** @var httpful\Response $response */
$response = \Httpful\Request::get($uri)
    ->sendsAndExpects('application/atom+xml')
    ->autoParse(false)
    ->send();

if ($response->hasErrors()) {
    pushVariable( 'HASH(AGI_REST_PHONEBOOK,ERRNO)', 255 );
    pushVariable( 'HASH(AGI_REST_PHONEBOOK,ERRMSG)', 'rest error' );
    fclose( STDIN );
    fclose( STDOUT );
    fclose( STDERR );
    exit( 1 );
}
$xml = simplexml_load_string($response->body, "SimpleXMLElement", 0, "", true);
$name = $xml->entry[0]->title;
if($name) {
    pushVariable('HTTP_CALLER_ID', $name);
}
fclose( STDIN );
fclose( STDOUT );
fclose( STDERR );
exit( 0 );

/**
 * create the right output to set a channel variable
 * @param $name
 * @param $value
 */
function pushVariable( $name, $value )
{
    fwrite( STDOUT, "SET VARIABLE " . trim( $name ) . " \"" . trim( $value ) . "\" 
" );
    fflush( STDOUT );
}

Remove the line with ***************

I’ve tested it and it works on my appliance

ich habe es heute an einer Neuinstallation getestet,
es funktioniert bei mir nicht …

Auch die hello world wird nicht ausgegeben.

It looks like that you have already resolved caller name.

Please try to change the dialplan script content and post here a CLI output


exten => s,1,Verbose(${CALLERID(name)})
same => n,GotoIf($ "${CALLERID(name)}" != "" ] ?skip)
same => n,AGI(agi_rest_phonebook.php,${MDC_CALLER_NUM_INTERNAT})
same => n,GoToIf($ "${AGISTATUS}" = "FAILURE" ]?skip)
same => n,ExecIf($ "${HTTP_CALLER_ID}" != "" ]?Verbose(1,"${HTTP_CALLER_ID}")
same => n,ExecIf($ "${HTTP_CALLER_ID}" != "" ]?Set(CALLERID(name)=${HTTP_CALLER_ID}))
same => n,Return()
same => n(skip),Return()


  -- Goto (mdc_ident-3,20-dial,1)
    -- Executing [20-dial@mdc_ident-3:1] Set("SIP/mdc_trunk_conf-1-00000037", "__MDC_EXTEN=20") in new stack
    -- Executing [20-dial@mdc_ident-3:2] Gosub("SIP/mdc_trunk_conf-1-00000037", "sub_prefix-20,ext,1") in new stack
    -- Executing [ext@sub_prefix-20:1] Gosub("SIP/mdc_trunk_conf-1-00000037", "def_agi_rest_phonebook,s,1") in new stack
    -- Executing [s@def_agi_rest_phonebook:1] Verbose("SIP/mdc_trunk_conf-1-00000037", "") in new stack
    -- Executing [s@def_agi_rest_phonebook:2] GotoIf("SIP/mdc_trunk_conf-1-00000037", "0 ?skip") in new stack
    -- Executing [s@def_agi_rest_phonebook:3] AGI("SIP/mdc_trunk_conf-1-00000037", "agi_rest_phonebook.php,004179xxxxxx") in new stack
    -- Launched AGI Script /etc/asterisk/agi/agi_rest_phonebook.php
    -- <SIP/mdc_trunk_conf-1-00000037>AGI Script agi_rest_phonebook.php completed, returning 0
    -- Executing [s@def_agi_rest_phonebook:4] GotoIf("SIP/mdc_trunk_conf-1-00000037", "1?skip") in new stack
    -- Goto (def_agi_rest_phonebook,s,8)
    -- Executing [s@def_agi_rest_phonebook:8] Return("SIP/mdc_trunk_conf-1-00000037", "") in new stack
    -- Executing [ext@sub_prefix-20:2] Return("SIP/mdc_trunk_conf-1-00000037", "") in new stack
    -- Executing [20-dial@mdc_ident-3:3] Gosub("SIP/mdc_trunk_conf-1-00000037", "sub_main-20,ext,1") in new stack
    -- Executing [ext@sub_main-20:1] Set("SIP/mdc_trunk_conf-1-00000037", "__PICKUPMARK=20") in new stack
    -- Executing [ext@sub_main-20:2] ExecIf("SIP/mdc_trunk_conf-1-00000037", "0?Set(__SPYGROUP=20:):Set(__SPYGROUP=20:079xxxxxx)") in new stack
    -- Executing [ext@sub_main-20:3] Dial("SIP/mdc_trunk_conf-1-00000037", "Local/20@mdc_diallocation") in new stack
    -- Called Local/20@mdc_diallocation
    -- Executing [20@mdc_diallocation:1] Verbose("Local/20@mdc_diallocation-0000005c;2", "1,dial to 20 for user 3") in new stack

Hmm, callerid is empty. Something is wrong within .php script. I tried on fresh 7.12.02 installation and it works out of the box. I am clueless, I will come back if I find something…

You can try to change agi_rest_phonebook.php
please replace


define ('__DEBUG__', true);

with


define ('__DEBUG__', false);

define (‘DEBUG’, false);

output :


 -- Executing [s@def_agi_rest_phonebook:1] Verbose("SIP/mdc_trunk_conf-1-00000005", "") in new stack
    -- Executing [s@def_agi_rest_phonebook:2] GotoIf("SIP/mdc_trunk_conf-1-00000005", "0 ?skip") in new stack
    -- Executing [s@def_agi_rest_phonebook:3] AGI("SIP/mdc_trunk_conf-1-00000005", "agi_rest_phonebook.php,004179xxxxxxx") in new stack
    -- Launched AGI Script /etc/asterisk/agi/agi_rest_phonebook.php
    -- <SIP/mdc_trunk_conf-1-00000005>AGI Script agi_rest_phonebook.php completed, returning 0
    -- Executing [s@def_agi_rest_phonebook:4] GotoIf("SIP/mdc_trunk_conf-1-00000005", "1?skip") in new stack
    -- Goto (def_agi_rest_phonebook,s,8)
    -- Executing [s@def_agi_rest_phonebook:8] Return("SIP/mdc_trunk_conf-1-00000005", "") in new stack
    -- Executing [ext@sub_prefix-20:2] Return("SIP/mdc_trunk_conf-1-00000005", "") in new stack
    -- Executing [20-dial@mdc_ident-3:3] Gosub("SIP/mdc_trunk_conf-1-00000005", "sub_main-20,ext,1") in new stack

:confused:

What does /var/log/syslog say? Do you see some error message?

keine Fehlermeldung in syslog …

Mal ne Frage in die Runde :wink:

Hat jemand dieses Script erfolgreich im Einsatz ?

Hallo,

ich möchte dieses Thema nochmals hochholen…

ich habe es trotz Stefan’s super Anleitung nicht geschafft dieses Script zur Abfrage des Telefonbucheintrags zu bewegen.
Ich habe es auf drei verschiedenen MD installationen versucht.

Hat jemand hier im Forum (ausser Stefan :wink: ) dieses Script erfolgreich im Einsatz ?

Hi Nada,

After a long time I noticed a problem in your script. $argv 1 ] should be $argv[1]. You have the same error on two places.

Kind regards,
Stefan

Hi Stefan,

das Script läuft bei mir nun auch !
Es war ein “Copy Paste” Problem… (Windows)

Vielen Dank für Deine Mühe !

Gruss Nada

Hallo ,

im aktuellen Client (17.10.R) wird der Name nicht mehr angezeigt …
Bei den Telefonen funktioniert es.

Hallo @nada,

das Skript was ich nutze funktioniert auch mit der 17.10 einwandfrei. Ich hatte mich damals an dem Skript von @Stefan aus diesem Thread orientiert.

Ändere das Skript mal wie folgt:

exten => s,1,Verbose(${CALLERID(name)})
same => n,GotoIf($[ "${CALLERID(name)}" != "" ] ?skip)
same => n,AGI(agi_rest_phonebook.php,${MDC_CALLER_NUM_INTERNAT})
same => n,GoToIf($[ "${AGISTATUS}" = "FAILURE" ]?skip)
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Verbose(1,"${HTTP_CALLER_ID}")
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(MDC_RESOLVENAME_HITS=1))
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(__MDC_DIALCALLERNAME=${HTTP_CALLER_ID}))
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(CALLERID(name)=${HTTP_CALLER_ID}))
same => n,Return()
same => n(skip),Return() 

Jetzt sollte es wieder funktionieren nehme ich an :slight_smile:

Mit freundlichen Grüßen
Stefan

Hi Stefan,

mit obigem Eintrag wird auch bei den Telefonen kein Name mehr angezeigt…
Mein aktuelles def_agi_rest_phonebook script :

#perform search only if name is not set
exten => s,1,GotoIf($[ "${CALLERID(name)}" != "" ] ?skip)
#you can use CALLERID(num) instead of MDC_CALLER_NUM_INTERNAT if you dont want full number
#invoke AGI 
same => n,AGI(agi_rest_phonebook.php,${MDC_CALLER_NUM_INTERNAT})
#check execution result
same => n,GoToIf($[ "${AGISTATUS}" = "FAILURE" ]?skip)
#AGI php script should set variable HTTP_CALLER_ID if matching result is found
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Verbose(1,"${HTTP_CALLER_ID}")
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(CALLERID(name)=${HTTP_CALLER_ID}))
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(MDC_LABEL-14=${HTTP_CALLER_ID}))
same => n,Return()
same => n(skip),Return()

Wie erwänht funktioniert das Script nur auf den Telefonen …
Im Client wird nur die Nummer angezeigt .

Hallo @nada,

in meinem Skript haben merkwürdigerweise ein paar eckige Klammern gefehlt…

Probiere es bitte noch ein Mal:

#perform search only if name is not set
exten => s,1,GotoIf($[ "${CALLERID(name)}" != "" ] ?skip)
#you can use CALLERID(num) instead of MDC_CALLER_NUM_INTERNAT if you dont want full number
#invoke AGI 
same => n,AGI(agi_rest_phonebook.php,${MDC_CALLER_NUM_INTERNAT})
#check execution result
same => n,GoToIf($[ "${AGISTATUS}" = "FAILURE" ]?skip)
#AGI php script should set variable HTTP_CALLER_ID if matching result is found
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Verbose(1,"${HTTP_CALLER_ID}")
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(MDC_RESOLVENAME_HITS=1))
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(__MDC_DIALCALLERNAME=${HTTP_CALLER_ID}))
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(CALLERID(name)=${HTTP_CALLER_ID}))
same => n,ExecIf($[ "${HTTP_CALLER_ID}" != "" ]?Set(MDC_LABEL-14=${HTTP_CALLER_ID}))
same => n,Return()
same => n(skip),Return()

Mit freundlichen Grüßen
Stefan

perfekt Danke !!! :sunglasses:

Heyy ich möchte das umsetzen, doch leider habe ich null Ahnung von PHP, kann mir jemand eine kurze Anleitung machen, wie ich das in die Pascom Cloud integrieren kann. Gerne möchte ich auch die Search Api benutzen.

Tausend dank

Freundliche Grüsse

Luca