Add support for slash commands

This commit is contained in:
Midgard 2022-06-09 00:29:24 +02:00
parent 0cc0cb1c13
commit 240745a404
Signed by: midgard
GPG key ID: 511C112F1331BBB4
3 changed files with 44 additions and 5 deletions

View file

@ -164,6 +164,25 @@ function sendMessage(endpoint, channel_id, message) {
//}); //});
} }
async function executeSlashCommand(endpoint, channel_id, command) {
const client = mm_client.get(endpoint);
const response = await client.executeSlashCommand(channel_id, command);
if (
response.responseJson &&
response.responseJson.text &&
response.responseJson.response_type !== "in_channel"
) {
pubsub.publish("MESSAGES_NEW", {
endpoint,
channel_id,
create_at: (new Date()).getTime(),
user_id: client.me.id,
message: "(only visible to you) " + response.responseJson.text
});
}
}
function markChannelAsRead(client, channel) { function markChannelAsRead(client, channel) {
return client.markChannelRead(channel.id); return client.markChannelRead(channel.id);
} }
@ -172,11 +191,22 @@ function checkKeyPress(event) {
// Battle tested for many years in several browsers // Battle tested for many years in several browsers
if ((event.keyCode === event.DOM_VK_RETURN || event.keyCode === 13 || event.keyCode === 10 || event.key === "Enter" || event.keyIdentifier === "U+000A") && !event.shiftKey && !event.ctrlKey) { if ((event.keyCode === event.DOM_VK_RETURN || event.keyCode === 13 || event.keyCode === 10 || event.key === "Enter" || event.keyIdentifier === "U+000A") && !event.shiftKey && !event.ctrlKey) {
if (byId("compose").value.length > 0) { if (byId("compose").value.length > 0) {
let message = byId("compose").value;
if (message[0] === "/") {
executeSlashCommand(
byId("channel_contents").dataset["server"],
byId("channel_contents").dataset["id"],
message
);
} else {
sendMessage( sendMessage(
byId("channel_contents").dataset["server"], byId("channel_contents").dataset["server"],
byId("channel_contents").dataset["id"], byId("channel_contents").dataset["id"],
byId("compose").value message
); );
}
byId("compose").value = ""; byId("compose").value = "";
} }
return true; return true;

View file

@ -155,6 +155,10 @@ class MattermostClient {
}); });
} }
executeSlashCommand(channel_id, command) {
return this.post("/commands/execute", {channel_id, command});
}
getUsers() { getUsers() {
if (!this.users) { if (!this.users) {
this.users = this.paginatedGet("/users", {}).then(users => { this.users = this.paginatedGet("/users", {}).then(users => {

View file

@ -72,6 +72,11 @@ function arrayToHashmap(array, key) {
} }
function startsWith(string, start) {
return string.slice(0, start.length) == start;
}
function padLeft(input, width, padding=" ") { function padLeft(input, width, padding=" ") {
text = input + ""; text = input + "";
while (text.length < width) { while (text.length < width) {