-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.php
More file actions
54 lines (46 loc) · 1.63 KB
/
Copy pathAPI.php
File metadata and controls
54 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\AskYourDatabase;
use Piwik\Piwik;
/**
* API for plugin AskYourDatabase
*
* @method static \Piwik\Plugins\AskYourDatabase\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* Creates an AskYourDatabase chatbot session for the current user.
*
* The chatbot can read the connected database, only super users can open it.
*
* @return array{url: string} one-time URL to open in the chatbot iframe
*/
public function createSession(): array
{
Piwik::checkUserHasSuperUserAccess();
$settings = new SystemSettings();
if (!$settings->isConfigured()) {
throw new \Exception(Piwik::translate('AskYourDatabase_NotConfigured'));
}
$name = trim((string) $settings->name->getValue());
$email = trim((string) $settings->email->getValue());
try {
$url = (new SessionClient())->createSession(
trim((string) $settings->apiKey->getValue()),
$settings->getChatbotId(),
$name !== '' ? $name : Piwik::getCurrentUserLogin(),
$email !== '' ? $email : (string) Piwik::getCurrentUserEmail()
);
} catch (\Exception $e) {
// not chained: the message already contains the cause, and the chain would be displayed to the user
throw new \Exception(Piwik::translate('AskYourDatabase_SessionError', [$e->getMessage()]));
}
return ['url' => $url];
}
}