2016-08-04 20:01:30 +03:00
< ? php
namespace App\Http\Controllers ;
2016-08-07 09:10:36 +03:00
use Exception ;
2016-08-10 15:57:34 +03:00
use App\Libraries\Skype\SkypeResponse ;
use App\Libraries\CurlUtils ;
2016-08-04 20:01:30 +03:00
use App\Ninja\Intents\BaseIntent ;
class BotController extends Controller
{
public function handleMessage ( $platform )
{
$to = '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts' ;
2016-08-10 15:57:34 +03:00
//$message = 'new invoice for john for 2 items due tomorrow';
//$message = 'create a new invoice for john smith for 2 tickets, set the invoice date to today, the due date to tomorrow, the deposit to 5 and the discount set to 10 percent';
//$message = 'create a new invoice for john smith with a due date of September 7th';
//$message = 'create a new invoice for john';
$message = 'add 2 tickets and set the due date to yesterday' ;
//$message = 'set the po number to 0004';
//$message = 'set the quantity to 20';
2016-08-09 23:06:24 +03:00
//$message = 'send the invoice';
2016-08-10 15:57:34 +03:00
//$message = 'show me my products';
2016-08-06 20:54:56 +03:00
2016-08-09 23:06:24 +03:00
echo " Message: $message <p> " ;
2016-08-07 22:42:32 +03:00
$token = $this -> authenticate ();
//try {
$state = $this -> loadState ( $token );
2016-08-07 09:10:36 +03:00
$data = $this -> parseMessage ( $message );
2016-08-07 22:42:32 +03:00
$intent = BaseIntent :: createIntent ( $state , $data );
2016-08-07 09:10:36 +03:00
$message = $intent -> process ();
2016-08-07 22:42:32 +03:00
$state = $intent -> getState ();
$this -> saveState ( $token , $state );
/*
2016-08-07 09:10:36 +03:00
} catch ( Exception $exception ) {
2016-08-10 15:57:34 +03:00
SkypeResponse :: message ( $exception -> getMessage ());
2016-08-07 09:10:36 +03:00
}
2016-08-07 22:42:32 +03:00
*/
$this -> sendResponse ( $token , $to , $message );
}
private function authenticate ()
{
$clientId = env ( 'MSBOT_CLIENT_ID' );
$clientSecret = env ( 'MSBOT_CLIENT_SECRET' );
$scope = 'https://graph.microsoft.com/.default' ;
$data = sprintf ( 'grant_type=client_credentials&client_id=%s&client_secret=%s&scope=%s' , $clientId , $clientSecret , $scope );
$response = CurlUtils :: post ( MSBOT_LOGIN_URL , $data );
$response = json_decode ( $response );
return $response -> access_token ;
}
private function loadState ( $token )
{
$url = sprintf ( '%s/botstate/skype/conversations/%s' , MSBOT_STATE_URL , '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts' );
$headers = [
'Authorization: Bearer ' . $token
];
2016-08-07 09:10:36 +03:00
2016-08-07 22:42:32 +03:00
$response = CurlUtils :: get ( $url , $headers );
$data = json_decode ( $response );
return json_decode ( $data -> data );
2016-08-07 09:10:36 +03:00
}
private function parseMessage ( $message )
{
2016-08-04 20:01:30 +03:00
$appId = env ( 'MSBOT_LUIS_APP_ID' );
$subKey = env ( 'MSBOT_LUIS_SUBSCRIPTION_KEY' );
$message = rawurlencode ( $message );
$url = sprintf ( '%s?id=%s&subscription-key=%s&q=%s' , MSBOT_LUIS_URL , $appId , $subKey , $message );
$data = file_get_contents ( $url );
$data = json_decode ( $data );
2016-08-07 09:10:36 +03:00
return $data ;
2016-08-04 20:01:30 +03:00
}
2016-08-07 22:42:32 +03:00
private function saveState ( $token , $data )
2016-08-04 20:01:30 +03:00
{
2016-08-07 22:42:32 +03:00
$url = sprintf ( '%s/botstate/skype/conversations/%s' , MSBOT_STATE_URL , '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts' );
2016-08-04 20:01:30 +03:00
2016-08-07 22:42:32 +03:00
$headers = [
'Authorization: Bearer ' . $token ,
'Content-Type: application/json' ,
2016-08-04 20:01:30 +03:00
];
2016-08-07 22:42:32 +03:00
$data = '{ eTag: "*", data: "' . addslashes ( json_encode ( $data )) . '" }' ;
2016-08-06 20:54:56 +03:00
2016-08-10 15:57:34 +03:00
CurlUtils :: post ( $url , $data , $headers );
2016-08-07 22:42:32 +03:00
}
2016-08-06 20:54:56 +03:00
2016-08-07 22:42:32 +03:00
private function sendResponse ( $token , $to , $message )
{
2016-08-04 20:01:30 +03:00
$url = sprintf ( '%s/conversations/%s/activities/' , SKYPE_API_URL , $to );
2016-08-07 22:42:32 +03:00
$headers = [
'Authorization: Bearer ' . $token ,
2016-08-04 20:01:30 +03:00
];
2016-08-07 22:42:32 +03:00
$response = CurlUtils :: post ( $url , $message , $headers );
2016-08-04 20:01:30 +03:00
2016-08-10 15:57:34 +03:00
echo " <pre> " . htmlentities ( json_encode ( json_decode ( $message ), JSON_PRETTY_PRINT )) . " </pre> " ;
2016-08-04 20:01:30 +03:00
var_dump ( $response );
}
}