Skip to content

Commit 0e1d688

Browse files
committed
✨ integration copilot with BotFramework-WebChat
add webchat framework integration parts in block_iagora.php and edit_form.php files, also add iagora.js file to manage javascript with moodle
1 parent 43e57e8 commit 0e1d688

File tree

7 files changed

+128
-14
lines changed

7 files changed

+128
-14
lines changed

amd/build/iagora.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/iagora.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/iagora.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import WebChat from 'botframework-webchat';
2+
/**
3+
* Initializes the chat with given parameters.
4+
* @param {Object} params - The initialization parameters.
5+
* @param {string} params.copiloturl - The copilot.
6+
* @param {string} params.categoryid - The category ID.
7+
* @param {string} params.chatId - The chat container ID.
8+
*/
9+
10+
export const init = ({copiloturl}) => {
11+
// eslint-disable-next-line no-console
12+
console.log('Copilot URL:', copiloturl);
13+
//initIagoraChat(chatId);
14+
};
15+
/**
16+
*
17+
* @param {string} chatId
18+
*/
19+
export function initIagoraChat(chatId) {
20+
const chatContainer = document.getElementById(chatId);
21+
22+
// Static token and directLine URL (example values)
23+
const directLineURL = 'https://directline.botframework.com';
24+
const token = chatId ;
25+
try {
26+
const directLine = WebChat.createDirectLine({domain: new URL("v3/directline", directLineURL), token});
27+
28+
const subscription = directLine.connectionStatus$.subscribe({
29+
next(value) {
30+
if (value === 2) {
31+
directLine
32+
.postActivity({
33+
localTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
34+
locale: document.documentElement.lang || "en",
35+
name: "startConversation",
36+
type: "event"
37+
})
38+
.subscribe();
39+
40+
subscription.unsubscribe();
41+
}
42+
}
43+
});
44+
45+
WebChat.renderWebChat({directLine, locale: document.documentElement.lang || "en"}, chatContainer);
46+
} catch (error) {
47+
// eslint-disable-next-line no-console
48+
console.error("Error initializing chat:", error);
49+
chatContainer.innerHTML = "Failed to load chat. Please try again later.";
50+
}
51+
}
52+
53+
export default {
54+
init,
55+
initIagoraChat
56+
};

block_iagora.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,59 @@ public function get_content() {
4848

4949
$this->content = new stdClass();
5050
$this->content->footer = '';
51-
5251
$iframeurl = isset($this->config->iframeurl) ? $this->config->iframeurl : '';
5352

54-
if (empty($iframeurl)) {
55-
$this->content->text = get_string('noiframeurl', 'block_iagora');
56-
} else {
57-
$this->content->text = '<iframe src="' . $iframeurl . '" width="100%" height="400px" frameborder="0"></iframe>';
58-
}
53+
// if (empty($iframeurl)) {
54+
// $this->content->text = get_string('noiframeurl', 'block_iagora');
55+
// } else {
56+
// $this->content->text = '<iframe src="' . $iframeurl . '" width="100%" height="400px" frameborder="0"></iframe>';
57+
// }
58+
$this->content->text = $this->generate_chat_content($iframeurl);
5959

6060
return $this->content;
6161
}
6262

6363
/**
6464
* Define in which pages this block can be added.
6565
*
66-
* @return array of the pages where the block can be added.
66+
* @param string $iframeurl The URL to be used in the iframe.
67+
* @return string The generated HTML content.
6768
*/
68-
public function applicable_formats() {
69-
return ['all' => true];
69+
private function generate_chat_content($iframeurl) {
70+
global $PAGE, $OUTPUT; die($iframeurl);
71+
72+
// Load necessary styles and scripts
73+
$PAGE->requires->js(new moodle_url('https://cdn.botframework.com/botframework-webchat/latest/webchat.js'), true);
74+
$PAGE->requires->js_call_amd('block_iagora/iagora', 'init', [[
75+
'copiloturl' => $iframeurl
76+
]]);
77+
78+
// Generate a unique identifier for the chat container
79+
$chat_id = uniqid('iagora_chat_');
80+
81+
// Chat button icon (using Moodle icons)
82+
$chat_icon = $OUTPUT->pix_icon('t/message', get_string('openchat', 'block_iagora'));
83+
84+
$output = html_writer::div($chat_icon, 'iagora-chat-toggle', ['id' => 'iagora-toggle-' . $chat_id,'data-url' => 'iframe_url']);
85+
$output .= html_writer::div('', 'iagora-chat-container', ['id' => $chat_id]);
86+
87+
// Add initialization script
88+
// $output .= html_writer::script("
89+
// document.addEventListener('DOMContentLoaded', function() {
90+
// initIagoraChat('{$chat_id}', " . json_encode($this->config) . ");
91+
// });
92+
// ");
93+
94+
return $output;
7095
}
7196

7297
/**
73-
* Allow the block to be configured.
98+
* Define in which pages this block can be added.
7499
*
75-
* @return bool
100+
* @return array of the pages where the block can be added.
76101
*/
77-
public function instance_allow_config() {
78-
return true;
102+
public function applicable_formats() {
103+
return ['all' => true];
79104
}
80105

81106
/**

edit_form.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ protected function specific_definition($mform) {
4545
$mform->setDefault('config_iframeurl', '');
4646
$mform->addElement('static', 'iframeurl_desc', '', get_string('iframeurl_desc', 'block_iagora'));
4747

48+
// Background color
49+
$mform->addElement('text', 'config_backgroundcolor', get_string('backgroundcolor', 'block_iagora'));
50+
$mform->setDefault('config_backgroundcolor', '#A9D1F3');
51+
$mform->setType('config_backgroundcolor', PARAM_TEXT);
52+
53+
// Bubble background color
54+
$mform->addElement('text', 'config_bubblebackground', get_string('bubblebackground', 'block_iagora'));
55+
$mform->setDefault('config_bubblebackground', '#F3CBA9');
56+
$mform->setType('config_bubblebackground', PARAM_TEXT);
4857
}
4958

5059
}

lang/en/block_iagora.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
$string['backgroundcolor'] = 'Background Color';
26+
$string['bubblebackground'] = 'Bubble Background Color';
27+
$string['pluginname'] = 'IAGORA';
2528
$string['iframeurl'] = 'Iframe URL';
2629
$string['iframeurl_desc'] = 'Public URL available for chat iframe';
2730
$string['iframeurl_help'] = 'Public URL for the Microsoft Copilot chat iframe';
2831
$string['noiframeurl'] = 'No url defined for this block. Please configure a URL in the block parameters';
29-
$string['pluginname'] = 'IAGORA';
32+
33+

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "iagora",
3+
"version": "1.0.0",
4+
"description": "iagora Chat block for Moodle",
5+
"author": "Datum Academy <[email protected]>",
6+
"license": "GPL-3.0",
7+
"private": true,
8+
"dependencies": {
9+
"botframework-webchat": "^4.15.0"
10+
},
11+
"scripts": {
12+
"build": "grunt amd",
13+
"watch": "grunt watch",
14+
"lint": "grunt eslint stylelint"
15+
}
16+
}

0 commit comments

Comments
 (0)