-
Notifications
You must be signed in to change notification settings - Fork 5
/
IbmChatApp.php
108 lines (88 loc) · 2.77 KB
/
IbmChatApp.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Drupal\helfi_platform_config\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a Watson chatbot block.
*
* @Block(
* id = "ibm_chat_app",
* admin_label = @Translation("IBM Chat App"),
* )
*/
class IbmChatApp extends BlockBase {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
// hostname: Hostname of chat application.
$form['hostname'] = [
'#type' => 'textfield',
'#title' => $this->t('Chat Hostname'),
'#default_value' => $config['hostname'] ?? '',
];
// engagementId: will define how our chat application looks and behaves.
$form['engagementId'] = [
'#type' => 'textfield',
'#title' => $this->t('Chat Engagement Id'),
'#default_value' => $config['engagementId'] ?? '',
];
// tenantId: defines the environment to be used.
$form['tenantId'] = [
'#type' => 'textfield',
'#title' => $this->t('Chat Tenant Id'),
'#default_value' => $config['tenantId'] ?? '',
];
// assistantId: identifies the bot instance to be used.
$form['assistantId'] = [
'#type' => 'textfield',
'#title' => $this->t('Chat Assistant Id'),
'#default_value' => $config['assistantId'] ?? '',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $formState) {
$this->configuration['hostname'] = $formState->getValue('hostname');
$this->configuration['engagementId'] = $formState->getValue('engagementId');
$this->configuration['tenantId'] = $formState->getValue('tenantId');
$this->configuration['assistantId'] = $formState->getValue('assistantId');
}
/**
* {@inheritdoc}
*/
public function build() {
$library = ['helfi_platform_config/chat_enhancer'];
$build = [];
$config = $this->getConfiguration();
$hostname = $config['hostname'];
$engagementId = $config['engagementId'];
$tenantId = $config['tenantId'];
$assistantId = $config['assistantId'];
$buttonSrc = sprintf('%s/get-widget-button?tenantId=%s&assistantId=%s&engagementId=%s', $hostname, $tenantId, $assistantId, $engagementId);
$build['ibm_chat_app'] = [
'#title' => $this->t('IBM Chat App'),
'#attached' => [
'library' => $library,
'html_head' => [
[
[
'#tag' => 'script',
'#attributes' => [
'async' => TRUE,
'type' => 'text/javascript',
'src' => $buttonSrc,
],
], 'chat_app_button',
],
],
],
];
return $build;
}
}