The Javascript API can only be used in pages loaded in Dialog and Sidebar.
1 2<!--Required --> <script src="https://www.hipchat.com/atlassian-connect/all.js"></script>
Details can be found here: Dialog
1 2HipChat.dialog.open({ key: "myaddon-dialog", // Use the dialog key declared in the descriptor options: dialogOptions, // Customize the dialog parameters: customParameters // Anything you want });
Details can be found here: Dialog
1 2HipChat.dialog.update(dialogOptions);
1 2HipChat.dialog.close();
1 2HipChat.sidebar.openView({ // references the key of the webPanel in the descriptor key: 'myaddon-sidebar-key', parameters: { // parameters to be passed to the sidebar view greetings: 'from somewhere else' } });
The parameters
argument can be received by the sidebar iframe by declaring an event handler:
1 2function receiveParameters(parameters) { // do something with the parameters } HipChat.register({ "receive-parameters": receiveParameters });
1 2HipChat.sidebar.closeView();
See Actions.
This works if the View the Javascript is run on is the target of the Action.
1 2HipChat.register({ // Use the key from the HipChat Action descriptor entry "myaddon-action-opendialog": function (message) { // In case of a Message Action, The HipChat message is passed as a parameter handle(message); } });
The following parameters are available on events triggered by message actions:
Parameter | Description |
---|---|
body | The message body |
sender.name | The name of the message sender |
sender.mention | The mention associated with the message sender |
sender.id | The id of the message sender |
mid | The message ID |
Example
1 2{ "body": "Hello", "sender": { "id": 356, "name": "Patrick Streule", "mention": "PatrickStreule" }, "mid": "7d8d27bb-4292-48eb-ba74-15efffe25050", "metadata": {} }
Parameter | Description |
---|---|
body | The message body |
metadata | Metadata provided as part of the card definition (if using a card) |
sender.name | The name of the message sender |
mid | The message ID |
card.title | The title of the card (if using a card) |
card.description | The description of the card (if using a card) |
Example
1 2{ "body": "<b>Fallback message</b>", "sender": { "name": "HCTester" }, "mid": "d45b077e-4d0d-4918-9a2c-55c96952898c", "metadata": { "foo": "bar" } }
This opens a "create room" dialog in the HipChat client, populated with the information you pass in.
1 2HipChat.room.create("The Room Name", "The Room Topic", "public/private");
1 2HipChat.chat.appendMessage('Hello!!');
1 2HipChat.chat.focus();
The image data is passed as a base64 data URI
1 2HipChat.chat.addFileForUploadWithBase64(base64DataUri, name);
Open a HipChat room (specifying a Sidebar to be opened is not possible yet)
1 2HipChat.room.open({ roomId: 123456 }); // or HipChat.room.open({ roomName: "my room name" });
1 2HipChat.room.open({ userId: 123456 }); // or HipChat.room.open({ userMentionName: "@admin" }); // or HipChat.user.open({ userId: 123456 }); // or HipChat.user.open({ userMentionName: "@admin" });
Either of the above APIs can also be called with a message to prepopulate the chat box with.
1 2HipChat.room.open({ roomId: 123456, message: "Hello!" }); //or HipChat.user.open({ userId: 123123, message: "Hello!" });
Works with user IDs and mention names
1 2HipChat.room.invite([342, 653, 782]); // or HipChat.room.invite(["@peter", "@paul", "@mary"]);
You can open a single file or a list of files.
1 2var file1 = { id: "id1", name: "File 1", url: "https://example.com/img/id1.png" } var file2 = { id: "id2", name: "File 2", url: "https://example.com/img/id2.png" } // Allow to navigate through file1 and file2 and show file1 HipChat.file.openInFileViewer(file1, [file1, file2]); // Show file1 HipChat.file.openInFileViewer(file1);
Your views may need to retrieve information from your backend via AJAX calls. In order to authenticate these calls, HipChat provides a convenience method returning a JWT which can be used to authenticate the AJAX call. You can verify these tokens like any another JWT you receive from HipChat.
1 2HipChat.auth.withToken(function(err, token) { if (err) { // error } else { $.ajax({ type: 'GET', url: '/data', headers: { 'Authorization': 'JWT ' + token }, ... }); } });
The following javascript APIs will allow you to request information from the HipChat client rather than making a REST call to the server. Access to these methods is restricted to add-ons with the correct scopes:
Module | Signature | Required Scopes |
---|---|---|
room | getParticipants(callback) | VIEW_ROOM |
room | getRoomDetails(callback) | VIEW_ROOM |
user | getCurrentUser(callback) | VIEW_ROOM |
Returns the list of participants in the current room grouped into guests and members.
1 2HipChat.room.getParticipants(function(err, success) { if (err) { // error } else { // success } });
Returns information for the currently opened room.
1 2HipChat.room.getRoomDetails(function(err, success) { if (err) { // error } else { // success } });
Returns information about the current user.
1 2HipChat.user.getCurrentUser(function(err, success) { if (err) { // error } else { // success } });
There are several strategies you can use to keep your view in sync with the rest of your integration.
The HipChat javascript API lets you register listeners for specific events in web panels. For example, your web panel can be notified when your glance is updated, or a message was received from your add-on in the room where the add-on web panel is open (so you can refresh or change the content of the web panel)
1 2HipChat.register({ "glance-update": function(data) { // Event is triggered on glance updates }, "message-received": function(data) { // Event is triggered when messages are received from your addon } });
HipChat Connect does not provide any other mechanisms for updating your sidebar. If you need more frequent / real time updates you may need to make use of web sockets or polling your backend servers.
Rate this page: