Needle Tags

Tag threads from Autothreader (needle)

Intro

I have been slowly working on a needle command, to put tags onto user threads, which can then be used to come up with basic stats and analytics.

Learning some basic Typescript, familiarizing with the needle bot code-base.

Learning about slash commands, the slash command builder…

Link to the command gist at the end of this post

Approach

I actually started this as a side-kick bot for needle. It’s own bot with its own commands. But after browsing around in the needle source code, it started to make sense (I know close to nothing about Typescript).

So I switched gears and dove into creating a new needle command.

I crammed all the tagging logic into just one file, so that including this new command into your setup would not be too much of a hazzle/inconvenience. It’s one command with a handful of subcommands.

There is one env variable that will be needed in your .env file.

.env

+ ES_DOMAIN=<url-to-elastic-search-domain>

The tagging data is persisted into an elasticsearch instance. For communication with the persistence layer, I went with axios

axios

npm i axios --save

Setup (with Searchly)

This section will go thru the process of getting a free elasticsearch in the cloud, but you can use your own if you have one spinning somewhere…

You can get a free-tier at searchly.com that provides everything that is needed.

Once on your searchly dashboard, navigate to Connection > API Connection & Key and locate your full domain url including the API Key:

dashboard fullaccess url ixmage{"ignore":true}

This is the value that you would put into your env variable ES_DOMAIN.

Create your Index

The searchly free-tier will allow you to create 2 indices before asking for an upgrade. We just need one index.

We will name the index threads

Do not create the index using their control panel. Instead we will PUT the index along with some property definitions.

Using your fullaccess URL (the one from screengrab above), we will PUT this data to

PUT <Your-Fullaccess-URL>/threads
{
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "properties": {
            "created": { "type": "date" },
            "updated": { "type": "date" },
            "tags": { "type": "keyword" }
        }
    }
}

Explicitly setting created and updated fields to date, and tags to keyword so queries and aggregations yield the expected outcome.

Postman example

postman put index ixmage{"ignore":true}

curl example

curl pending ixmage{"ignore":true}

Data about a thread

The data saved for a thread is

fieldinfo
uidThe thread’s discord channel id.
serverThe thread’s discord guild id.
createdWhen the tagging command was issued.
updateWhen the latest tagging command was issued.
statusNo real purpose at the moment.
tagsArray of tags for the thread.
logArray of tagging actions performed on this thread.
"_source": {
    "uid": "978152125286739968",
    "server": "620112554211147807",
    "created": "2022-05-23T04:36:45.983Z",
    "updated": "2022-05-23T04:36:45.983Z",
    "log": [
        {
            "who": "readonlychild",
            "av": "https://cdn...976d37.webp",
            "when": "2022-05-24T04:28:43.658Z",
            "what": "add: unk-test"
        }
    ],
    "tags": [
        "vue",
        "cli",
        "tailwind-css",
        "css",
        "unk-test"
    ],
    "status": "blocker"
}

Commands

Command List ixmage{"ignore":true}

/tags view

tags view ixmage{"ignore":true}

List the current thread tags

Command requires MANAGE_THREADS permission.
Command only works on thread channels

/tags clear

tags clear ixmage{"ignore":true}

Clear tags for the current thread

Command requires MANAGE_THREADS permission.
Command only works on thread channels

/tags add

tags add ixmage{"ignore":true}

Add a list of tags to the current thread

/tags replace

tags replace ixmage{"ignore":true}

Replace tags for the current thread

Command requires MANAGE_THREADS permission.
Command only works on thread channels

/tags status

tags status ixmage{"ignore":true}

Set a status for the current thread

Command requires MANAGE_THREADS permission.
Command only works on thread channels

/tags stats-top

tags stats-top ixmage{"ignore":true}

Command does not require any permissions.

List top 25 tags

tags stats-top days-back 10 ixmage{"ignore":true}

Conclusion

curl pending ixmage{"ignore":true}

Here is the gist for the command.

What next

Add logic to allow someone without permission MANAGE_THREADS but does have a role like support-squad to also use commands
/tags view | add | replace | clear


Searchly let’s you create other access endpoints, and one option is to make it read-only, so it is a safe endpoint to use client-side.

I may create one or a couple of astro components to display some statistics about the tags .



Back to Post List
ok!