2016-08-04 19:01:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
use App\Libraries\CurlUtils;
|
2016-08-07 08:10:36 +02:00
|
|
|
use Exception;
|
2016-08-04 19:01:30 +02:00
|
|
|
use App\Ninja\Intents\BaseIntent;
|
|
|
|
|
|
|
|
class BotController extends Controller
|
|
|
|
{
|
|
|
|
public function handleMessage($platform)
|
|
|
|
{
|
|
|
|
$to = '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts';
|
2016-08-09 16:14:26 +02:00
|
|
|
//$message = 'create a new invoice for Jenifer Altenwerth ';
|
|
|
|
$message = 'add 2 items';
|
2016-08-07 21:42:32 +02:00
|
|
|
//$message = view('bots.skype.message', ['message' => $message])->render();
|
2016-08-06 19:54:56 +02:00
|
|
|
//return $this->sendResponse($to, $message);
|
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
$token = $this->authenticate();
|
|
|
|
|
|
|
|
//try {
|
|
|
|
$state = $this->loadState($token);
|
|
|
|
var_dump($state);
|
|
|
|
|
2016-08-07 08:10:36 +02:00
|
|
|
$data = $this->parseMessage($message);
|
2016-08-07 21:42:32 +02:00
|
|
|
|
|
|
|
$intent = BaseIntent::createIntent($state, $data);
|
2016-08-07 08:10:36 +02:00
|
|
|
$message = $intent->process();
|
2016-08-07 21:42:32 +02:00
|
|
|
$state = $intent->getState();
|
|
|
|
|
|
|
|
$this->saveState($token, $state);
|
|
|
|
/*
|
2016-08-07 08:10:36 +02:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
$message = view('bots.skype.message', [
|
|
|
|
'message' => $exception->getMessage()
|
|
|
|
])->render();
|
|
|
|
}
|
2016-08-07 21:42:32 +02: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 08:10:36 +02:00
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
$response = CurlUtils::get($url, $headers);
|
|
|
|
$data = json_decode($response);
|
|
|
|
|
|
|
|
return json_decode($data->data);
|
2016-08-07 08:10:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function parseMessage($message)
|
|
|
|
{
|
2016-08-04 19:01:30 +02: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-09 16:14:26 +02:00
|
|
|
var_dump($data);
|
2016-08-04 19:01:30 +02:00
|
|
|
|
2016-08-07 08:10:36 +02:00
|
|
|
return $data;
|
2016-08-04 19:01:30 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
private function saveState($token, $data)
|
2016-08-04 19:01:30 +02:00
|
|
|
{
|
2016-08-07 21:42:32 +02:00
|
|
|
$url = sprintf('%s/botstate/skype/conversations/%s', MSBOT_STATE_URL, '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts');
|
|
|
|
var_dump($url);
|
2016-08-04 19:01:30 +02:00
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
$headers = [
|
|
|
|
'Authorization: Bearer ' . $token,
|
|
|
|
'Content-Type: application/json',
|
2016-08-04 19:01:30 +02:00
|
|
|
];
|
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
$data = '{ eTag: "*", data: "' . addslashes(json_encode($data)) . '" }';
|
2016-08-08 16:45:37 +02:00
|
|
|
//$data = '{ eTag: "*", data: "" }';
|
2016-08-07 21:42:32 +02:00
|
|
|
var_dump($data);
|
|
|
|
$response = CurlUtils::post($url, $data, $headers);
|
2016-08-06 19:54:56 +02:00
|
|
|
|
|
|
|
var_dump($response);
|
2016-08-07 21:42:32 +02:00
|
|
|
}
|
2016-08-06 19:54:56 +02:00
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
private function sendResponse($token, $to, $message)
|
|
|
|
{
|
2016-08-04 19:01:30 +02:00
|
|
|
$url = sprintf('%s/conversations/%s/activities/', SKYPE_API_URL, $to);
|
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
$headers = [
|
|
|
|
'Authorization: Bearer ' . $token,
|
2016-08-04 19:01:30 +02:00
|
|
|
];
|
|
|
|
|
2016-08-07 21:42:32 +02:00
|
|
|
$response = CurlUtils::post($url, $message, $headers);
|
2016-08-04 19:01:30 +02:00
|
|
|
|
|
|
|
var_dump($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|