Add or Remove ChatIDs to a TICK Script Automatically

I’m trying to allow a user to “opt-in” or “opt-out” of receiving alerts via Telegram.
So far I know that I can have multiple ChatIDs per alerts as follows:

var triggerTemp = temp
|alert()
.crit(lambda: “temp” > upperTempCrit OR “temp” < lowerTempCrit)
.message(deviceName + ’
Temperature changed to{{if eq .Level “CRITICAL”}} CRITICAL {{else if eq .Level “OK”}} OK {{end}}
Reading: {{ index .Fields “temp” }}°C
Time: {{ .Time.Local.Format “Mon, Jan 2 2006 at 15:04:05 MST” }}’)
.id(idVar)
.idTag(idTag)
.levelTag(levelTag)
.messageField(messageField)
.durationField(durationField)
.stateChangesOnly()
.telegram()
.chatId(‘abcd’)
.telegram()
.chatId(‘1234’)

Is there a way to add/remove/edit these TICK scripts with data from a database or text file or something similar so that when a user opts in or out they can be added or removed?

have you tried the sideload node ?
This allows the tick script to read in data from a configuration file.
I have used the functionality to read in maintenance windows, and server specific thresholds, but not to add a variable number of chat ids.

Alternatively you can define these scripts via the kapacitor API, so could you periodically read your database of subscribers and then using the API delete and re-define the tick script with the desired subscribers ?

https://docs.influxdata.com/kapacitor/v1.6/working/api/#manage-tasks

I found the sideload docs, but I’m not 100% sure how I could use it to add or change the TICK script, could you perhaps provide an example of how you got yours working?

I have used sideload node to override thresholds. So in my check_cpu script I have warning_threshold=70 and crit_threshold=90
for each server that I want to change these values I create a .yml file with the contents:
warning_threshold:20
crit_threshold:30
The sideload looks like:
stream from…
|sideload()
.source(file:///sideload)
.order(‘cpu/{{hostname}}.yml’)
.field(‘warning_threshold’,70)
.field(‘crit_threshold’,90)
| alert
.crit(lambda: (“cpu” >“crit_threshold”))
.warn(lambda:(“cpu” >“warning_threshold”))

Could this work ?

My second suggestion was to use an external program to create a task based on who was interested.
Assuming you had the names in a DB you could create a task per receiver using the Define or update task API

https://docs.influxdata.com/kapacitor/v1.6/working/api/#define-or-update-a-task

then the pseudo code would look like:
//delete all existing tasks
delete_all_tasks()
For each name in get_names()
POST /kapacitor/v1/tasks
{ “id” : “send_to_$name”,
“template-id” : “mytemplate”
“vars” :{
“receiver” : {
“value” : $name
“type” : string
}
}