2017-06-05 22:31:30 +02:00
|
|
|
// This function subscribes the newly created bot to the stream specified by the user.
|
|
|
|
function subscribe_to_stream(bot_email, stream_name) {
|
|
|
|
var bot_user_id = people.get_user_id(bot_email);
|
|
|
|
stream_data.add_subscriber(stream_name, bot_user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function puts together the main string for url that will be copied by the user
|
|
|
|
// to use for incoming webhook services.
|
2017-10-30 22:04:15 +01:00
|
|
|
function put_key_values_in_url(api_url, integration_url,
|
2017-06-05 22:31:30 +02:00
|
|
|
bot_api_key, stream_name) {
|
2017-10-30 22:04:15 +01:00
|
|
|
var integration_bot_url = api_url + integration_url +
|
2017-06-05 22:31:30 +02:00
|
|
|
"?api_key=" + bot_api_key +
|
|
|
|
"&stream=" + stream_name;
|
|
|
|
return integration_bot_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function returns the div/span where the integration bot url string will be
|
|
|
|
// set for the user.
|
|
|
|
function get_url_span() {
|
|
|
|
// This div or span value can be updated along with building the UI using this widget.
|
|
|
|
return $("#integration_bot_url");
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_integration_bot_url(integration_bot_url) {
|
|
|
|
var url_span = get_url_span();
|
|
|
|
url_span.text(integration_bot_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the function that runs after the bot is created successfully.
|
2017-10-30 22:04:15 +01:00
|
|
|
function on_create_bot_success(result, stream_name, api_url, integration_url) {
|
2017-06-05 22:31:30 +02:00
|
|
|
var bot_api_key = result.api_key;
|
|
|
|
var bot_email = result.email;
|
|
|
|
|
|
|
|
subscribe_to_stream(bot_email, stream_name);
|
2017-10-30 22:04:15 +01:00
|
|
|
var integration_bot_url = put_key_values_in_url(api_url, integration_url,
|
2017-06-05 22:31:30 +02:00
|
|
|
bot_api_key, stream_name);
|
|
|
|
update_integration_bot_url(integration_bot_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_bot(full_name, short_name, bot_avatar_file, stream_name,
|
2017-10-30 22:04:15 +01:00
|
|
|
api_url, integration_url, on_success) {
|
2017-06-05 22:31:30 +02:00
|
|
|
var formData = new FormData();
|
|
|
|
formData.append('csrfmiddlewaretoken', csrf_token);
|
|
|
|
formData.append('full_name', full_name);
|
|
|
|
formData.append('short_name', short_name);
|
2018-06-04 21:13:07 +02:00
|
|
|
formData.append('file-' + 0, bot_avatar_file);
|
2017-06-05 22:31:30 +02:00
|
|
|
channel.post({
|
|
|
|
url: '/json/bots',
|
|
|
|
data: formData,
|
|
|
|
cache: false,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
success: function (resp, statusText, xhr) {
|
|
|
|
var result = JSON.parse(xhr.responseText);
|
2017-10-30 22:04:15 +01:00
|
|
|
on_success(result, stream_name, api_url, integration_url);
|
2017-06-05 22:31:30 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
// UI yet to be created with this div id
|
|
|
|
$('#bot_widget_error').text(JSON.parse(xhr.responseText).msg).show();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the main function to be called to set the integration bot url.
|
|
|
|
exports.set_integration_bot_url = function (
|
2017-10-30 22:04:15 +01:00
|
|
|
api_url, integration_url, bot_full_name,
|
2017-06-05 22:31:30 +02:00
|
|
|
bot_short_name, bot_avatar_file, bot_owner, stream_name
|
|
|
|
) {
|
|
|
|
create_bot(bot_full_name, bot_short_name, bot_avatar_file,
|
2017-10-30 22:04:15 +01:00
|
|
|
stream_name, api_url, integration_url, on_create_bot_success);
|
2017-06-05 22:31:30 +02:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.integration_bot_widget = exports;
|