Skip to content

Instantly share code, notes, and snippets.

@zefhemel
Created March 26, 2021 08:32
Show Gist options
  • Save zefhemel/83f294e284f8e313e8f4aa0c802a0e37 to your computer and use it in GitHub Desktop.
Save zefhemel/83f294e284f8e313e8f4aa0c802a0e37 to your computer and use it in GitHub Desktop.
A simple reaction tracker in Matterless

Environment

server: https://community.mattermost.com
me_token: XXX
bot_token: XXX

MattermostClient: TrackerClient

url: $server
token: $me_token
events:
  reaction_added:
    - ReactionChange
  reaction_removed:
    - ReactionChange

Function: ReactionChange

import {Mattermost} from "matterless";

let meClient = new Mattermost(process.env.server, process.env.me_token);
let botClient = new Mattermost(process.env.server, process.env.bot_token);
let me, bot, directChannel;
async function init() {
    me = await meClient.getMeCached();
    bot = await botClient.getMeCached();;
    directChannel = await botClient.createDirectChannel([bot.id, me.id]);
}

async function handle(event) {
    console.log("Event", event);
    let reaction = JSON.parse(event.data.reaction);
    let reacter = await meClient.getUserCached(reaction.user_id);
    let post = await meClient.getPost(reaction.post_id);
    if(post.user_id !== me.id) {
       console.log("Not mine");
       return;
    }
    await botClient.createPost({
        channel_id: directChannel.id,
        message: `${event.event} :${reaction.emoji_name}: by @${reacter.username} to your post "${post.message}`
    });
    console.log("Sent!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment