2023-08-14 13:44:23 +00:00
---
title: "Templating"
---
2021-11-19 21:53:30 +00:00
2023-08-14 13:44:23 +00:00
{{< lead > }}
2021-11-19 21:53:30 +00:00
Generally speaking the templating uses [Golang `text/template` ](https://pkg.go.dev/text/template ) template syntax. All fields with templating enabled do support the full synax from the `text/template` package.
2023-08-14 13:44:23 +00:00
{{< / lead > }}
2021-11-19 21:53:30 +00:00
2023-08-14 13:44:23 +00:00
## Variables
2021-11-19 21:53:30 +00:00
There are certain variables available in the strings with templating enabled:
- `channel` - Channel the message was sent to, only available for regular messages not events
- `msg` - The message object, used in functions, should not be sent to chat
- `permitTimeout` - Value of `permit_timeout` in seconds
- `username` - The username of the message author
2023-08-14 13:44:23 +00:00
## Functions
2021-11-19 21:53:30 +00:00
2022-10-02 13:13:49 +00:00
Within templates following functions can be used:
- built-in functions in `text/template` engine
- functions from [sprig ](https://masterminds.github.io/sprig/ ) function collection
- functions mentioned below
2021-11-19 21:53:30 +00:00
Examples below are using this syntax in the code block:
```
! Message matcher used for the input message
> Input message if used in the example
# Template used in the fields
2023-08-25 21:37:37 +00:00
< Output from the template ( Rendered during docs generation )
* Output from the template (Static output, template not rendered)
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `arg`
2021-11-19 21:53:30 +00:00
Takes the message sent to the channel, splits by space and returns the Nth element
Syntax: `arg <index>`
Example:
```
> !bsg @tester
# {{ arg 1 }} please refrain from BSG
< @tester please refrain from BSG
```
2023-08-25 11:14:23 +00:00
### `b64urldec`
Decodes the input using base64 URL-encoding (like `b64dec` but using `URLEncoding` instead of `StdEncoding` )
Syntax: `b64urldec <input>`
Example:
```
# {{ b64urldec "bXlzdHJpbmc=" }}
< mystring
```
### `b64urlenc`
Encodes the input using base64 URL-encoding (like `b64enc` but using `URLEncoding` instead of `StdEncoding` )
Syntax: `b64urlenc <input>`
Example:
```
# {{ b64urlenc "mystring" }}
< bXlzdHJpbmc =
```
2023-08-14 13:44:23 +00:00
### `botHasBadge`
2021-11-19 21:53:30 +00:00
Checks whether bot has the given badge in the current channel
Syntax: `botHasBadge <badge>`
Example:
```
# {{ botHasBadge "moderator" }}
2023-10-26 16:39:20 +00:00
< false
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `channelCounter`
2021-11-19 21:53:30 +00:00
Wraps the counter name into a channel specific counter name including the channel name
Syntax: `channelCounter <counter name>`
Example:
```
# {{ channelCounter "test" }}
2023-08-25 21:37:37 +00:00
< #example:test
2021-11-19 21:53:30 +00:00
```
2023-10-26 16:39:20 +00:00
### `chatterHasBadge`
Checks whether chatter writing the current line has the given badge in the current channel
Syntax: `chatterHasBadge <badge>`
Example:
```
# {{ chatterHasBadge "moderator" }}
< true
```
2023-10-23 18:28:58 +00:00
### `counterRank`
Returns the rank of the given counter and the total number of counters in given counter prefix
Syntax: `counterRank <prefix> <name>`
Example:
```
# {{ $cr := counterRank (list .channel "test" "" | join ":") (list .channel "test" "foo" | join ":") }}{{ $cr.Rank }}/{{ $cr.Count }}
* 2/6
```
### `counterTopList`
Returns the top n counters for the given prefix as objects with Name and Value fields
Syntax: `counterTopList <prefix> <n>`
Example:
```
# {{ range (counterTopList (list .channel "test" "" | join ":") 3) }}{{ .Name }}: {{ .Value }} - {{ end }}
* #example:test:foo: 5 - #example:test:bar: 4 -
```
2023-08-14 13:44:23 +00:00
### `counterValue`
2021-11-19 21:53:30 +00:00
Returns the current value of the counter which identifier was supplied
Syntax: `counterValue <counter name>`
Example:
```
2023-04-22 19:09:44 +00:00
# {{ counterValue (list .channel "test" | join ":") }}
2023-08-25 21:37:37 +00:00
* 5
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `counterValueAdd`
2023-06-12 21:48:22 +00:00
Adds the given value (or 1 if no value) to the counter and returns its new value
Syntax: `counterValueAdd <counter name> [increase=1]`
Example:
2023-08-25 21:37:37 +00:00
2023-06-12 21:48:22 +00:00
```
# {{ counterValueAdd "myCounter" }} {{ counterValueAdd "myCounter" 5 }}
2023-08-25 21:37:37 +00:00
* 1 6
2023-06-12 21:48:22 +00:00
```
2023-08-14 13:44:23 +00:00
### `displayName`
2021-11-19 21:53:30 +00:00
Returns the display name the specified user set for themselves
Syntax: `displayName <username> [fallback]`
Example:
```
# {{ displayName "luziferus" }} - {{ displayName "notexistinguser" "foobar" }}
2023-08-25 21:37:37 +00:00
* Luziferus - foobar
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `doesFollow`
2022-12-26 21:38:14 +00:00
2023-09-29 12:54:24 +00:00
Returns whether `from` follows `to` (the bot must be moderator of `to` to read this)
2022-12-26 21:38:14 +00:00
Syntax: `doesFollow <from> <to>`
Example:
```
# {{ doesFollow "tezrian" "luziferus" }}
2023-08-25 21:37:37 +00:00
* true
2022-12-26 21:38:14 +00:00
```
2023-08-14 13:44:23 +00:00
### `doesFollowLongerThan`
2022-12-26 21:38:14 +00:00
2023-09-29 12:54:24 +00:00
Returns whether `from` follows `to` for more than `duration` (the bot must be moderator of `to` to read this)
2022-12-26 21:38:14 +00:00
Syntax: `doesFollowLongerThan <from> <to> <duration>`
Example:
```
# {{ doesFollowLongerThan "tezrian" "luziferus" "168h" }}
2023-08-25 21:37:37 +00:00
* true
2022-12-26 21:38:14 +00:00
```
2023-08-14 13:44:23 +00:00
### `fixUsername`
2021-11-19 21:53:30 +00:00
Ensures the username no longer contains the `@` or `#` prefix
Syntax: `fixUsername <username>`
Example:
```
# {{ fixUsername .channel }} - {{ fixUsername "@luziferus" }}
2023-08-25 21:37:37 +00:00
< example - luziferus
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `followAge`
2022-12-26 20:19:48 +00:00
2023-09-29 12:54:24 +00:00
Looks up when `from` followed `to` and returns the duration between then and now (the bot must be moderator of `to` to read this)
2022-12-26 20:19:48 +00:00
Syntax: `followAge <from> <to>`
Example:
```
# {{ followAge "tezrian" "luziferus" }}
2023-08-25 21:37:37 +00:00
* 15004h14m59.116620989s
2022-12-26 20:19:48 +00:00
```
2023-08-14 13:44:23 +00:00
### `followDate`
2021-11-19 21:53:30 +00:00
2023-09-29 12:54:24 +00:00
Looks up when `from` followed `to` (the bot must be moderator of `to` to read this)
2021-11-19 21:53:30 +00:00
Syntax: `followDate <from> <to>`
Example:
```
# {{ followDate "tezrian" "luziferus" }}
2023-08-25 21:37:37 +00:00
* 2021-04-10 16:07:07 +0000 UTC
```
### `formatDuration`
Returns a formated duration. Pass empty strings to leave out the specific duration part.
Syntax: `formatDuration <duration> <hours> <minutes> <seconds>`
Example:
```
# {{ formatDuration .testDuration "hours" "minutes" "seconds" }} - {{ formatDuration .testDuration "hours" "minutes" "" }}
< 5 hours , 33 minutes , 12 seconds - 5 hours , 33 minutes
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `group`
2021-11-19 21:53:30 +00:00
Gets matching group specified by index from `match_message` regular expression, when `fallback` is defined, it is used when group has an empty match
Syntax: `group <idx> [fallback]`
Example:
```
2023-08-25 21:37:37 +00:00
! !command ([0-9]+) ([a-z]+) ?([a-z]*)
2021-11-19 21:53:30 +00:00
> !command 12 test
# {{ group 2 "oops" }} - {{ group 3 "oops" }}
< test - oops
```
2023-09-03 10:25:31 +00:00
### `idForUsername`
Returns the user-id for the given username
Syntax: `idForUsername <username>`
Example:
```
# {{ idForUsername "twitch" }}
* 12826
```
2023-08-14 13:44:23 +00:00
### `inList`
2022-06-20 19:41:53 +00:00
Tests whether a string is in a given list of strings (for conditional templates).
2023-08-25 21:37:37 +00:00
Syntax: `inList <search> <...string>`
2022-06-20 19:41:53 +00:00
Example:
```
! !command (.*)
> !command foo
# {{ inList (group 1) "foo" "bar" }}
< true
```
2023-08-14 13:44:23 +00:00
### `jsonAPI`
2022-09-26 21:32:48 +00:00
Fetches remote URL and applies jq-like query to it returning the result as string. (Remote API needs to return status 200 within 5 seconds.)
2023-08-25 21:37:37 +00:00
Syntax: `jsonAPI <url> <jq-like path> [fallback]`
2022-09-26 21:32:48 +00:00
Example:
```
2023-08-25 21:37:37 +00:00
# {{ jsonAPI "https://api.github.com/repos/Luzifer/twitch-bot" ".owner.login" }}
* Luzifer
2022-09-26 21:32:48 +00:00
```
2023-08-14 13:44:23 +00:00
### `lastPoll`
2023-05-21 13:24:43 +00:00
Gets the last (currently running or archived) poll for the given channel (the channel must have given extended permission for poll access!)
Syntax: `lastPoll <channel>`
Example:
```
# Last Poll: {{ (lastPoll .channel).Title }}
2023-08-25 21:37:37 +00:00
* Last Poll: Und wie siehts im Template aus?
2023-05-21 13:24:43 +00:00
```
See schema of returned object in [`pkg/twitch/polls.go#L13` ](https://github.com/Luzifer/twitch-bot/blob/master/pkg/twitch/polls.go#L13 )
2023-08-14 13:44:23 +00:00
### `lastQuoteIndex`
2021-11-19 21:53:30 +00:00
Gets the last quote index in the quote database for the current channel
Syntax: `lastQuoteIndex`
Example:
```
# Last Quote: #{{ lastQuoteIndex }}
2023-08-25 21:37:37 +00:00
* Last Quote: #32
2022-07-15 19:06:32 +00:00
```
2023-08-14 13:44:23 +00:00
### `mention`
2023-05-27 19:58:58 +00:00
Strips username and converts into a mention
Syntax: `mention <username>`
Example:
```
# {{ mention "@user" }} {{ mention "user" }} {{ mention "#user" }}
< @user @user @user
```
2023-08-14 13:44:23 +00:00
### `pow`
2022-02-10 00:12:57 +00:00
Returns float from calculation: `float1 ** float2`
Syntax: `pow <float1> <float2>`
Example:
```
2023-08-25 21:37:37 +00:00
# {{ printf "%.0f" (pow 10 4) }}
2022-02-10 00:12:57 +00:00
< 10000
```
2023-08-25 21:37:37 +00:00
### `profileImage`
2023-08-18 15:48:20 +00:00
Gets the URL of the given users profile image
Syntax: `profileImage <username>`
Example:
```
# {{ profileImage .username }}
2023-08-25 21:37:37 +00:00
* https://static-cdn.jtvnw.net/jtv_user_pictures/[...].png
2023-08-18 15:48:20 +00:00
```
2023-08-14 13:44:23 +00:00
### `randomString`
2022-08-16 15:55:23 +00:00
Randomly picks a string from a list of strings
2023-08-25 21:37:37 +00:00
Syntax: `randomString <string> [...string]`
2022-08-16 15:55:23 +00:00
Example:
```
# {{ randomString "a" "b" "c" "d" }}
2023-08-25 21:37:37 +00:00
* a
2022-08-16 15:55:23 +00:00
```
2023-08-14 13:44:23 +00:00
### `recentGame`
2021-11-19 21:53:30 +00:00
Returns the last played game name of the specified user (see shoutout example) or the `fallback` if the game could not be fetched. If no fallback was supplied the message will fail and not be sent.
Syntax: `recentGame <username> [fallback]`
Example:
```
# {{ recentGame "luziferus" "none" }} - {{ recentGame "thisuserdoesnotexist123" "none" }}
2023-08-25 21:37:37 +00:00
* Metro Exodus - none
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `recentTitle`
2023-01-28 23:41:36 +00:00
Returns the last stream title of the specified user or the `fallback` if the title could not be fetched. If no fallback was supplied the message will fail and not be sent.
Syntax: `recentTitle <username> [fallback]`
Example:
```
# {{ recentGame "luziferus" "none" }} - {{ recentGame "thisuserdoesnotexist123" "none" }}
2023-08-25 21:37:37 +00:00
* Die Oper haben wir überlebt, mal sehen was uns sonst noch alles töten möchte… - none
2023-01-28 23:41:36 +00:00
```
2023-11-05 12:15:42 +00:00
### `scheduleSegments`
Returns the next n segments in the channels schedule. If n is not given, returns all known segments.
Syntax: `scheduleSegments <channel> [n]`
Example:
```
# {{ $seg := scheduleSegments "luziferus" 1 | first }}Next Stream: {{ $seg.Title }} @ {{ dateInZone "2006-01-02 15:04" $seg.StartTime "Europe/Berlin" }}
* Next Stream: Little Nightmares @ 2023-11-05 18:00
```
2023-08-14 13:44:23 +00:00
### `seededRandom`
2023-04-22 20:23:35 +00:00
Returns a float value stable for the given seed
Syntax: `seededRandom <string-seed>`
Example:
```
2023-08-25 21:37:37 +00:00
# Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}%
2023-11-05 12:15:42 +00:00
< Your int this hour: 73 %
2023-04-22 20:23:35 +00:00
```
2023-08-14 13:44:23 +00:00
### `streamUptime`
2021-11-19 21:53:30 +00:00
Returns the duration the stream is online (causes an error if no current stream is found)
Syntax: `streamUptime <username>`
Example:
```
# {{ formatDuration (streamUptime "luziferus") "hours" "minutes" "" }}
2023-08-25 21:37:37 +00:00
* 3 hours, 56 minutes
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `subCount`
2021-11-19 21:53:30 +00:00
2023-04-22 20:23:35 +00:00
Returns the number of subscribers (accounts) currently subscribed to the given channel
2021-11-19 21:53:30 +00:00
2023-04-22 20:23:35 +00:00
Syntax: `subCount <channel>`
2021-11-19 21:53:30 +00:00
Example:
```
2023-04-22 20:23:35 +00:00
# {{ subCount "luziferus" }}
2023-08-25 21:37:37 +00:00
* 26
2023-04-22 20:23:35 +00:00
```
2023-08-14 13:44:23 +00:00
### `subPoints`
2023-04-22 20:23:35 +00:00
Returns the number of sub-points currently given through the T1 / T2 / T3 subscriptions to the given channel
Syntax: `subPoints <channel>`
Example:
```
# {{ subPoints "luziferus" }}
2023-08-25 21:37:37 +00:00
* 26
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `tag`
2021-11-19 21:53:30 +00:00
Takes the message sent to the channel, returns the value of the tag specified
Syntax: `tag <tagname>`
Example:
```
2023-08-25 21:37:37 +00:00
# {{ tag "display-name" }}
< ExampleUser
2021-11-19 21:53:30 +00:00
```
2023-08-14 13:44:23 +00:00
### `textAPI`
2023-07-15 21:54:22 +00:00
Fetches remote URL and returns the result as string. (Remote API needs to return status 200 within 5 seconds.)
2023-08-25 21:37:37 +00:00
Syntax: `textAPI <url> [fallback]`
2023-07-15 21:54:22 +00:00
Example:
```
! !weather (.*)
> !weather Hamburg
# {{ textAPI (printf "https://api.scorpstuff.com/weather.php?units=metric&city=%s" (urlquery (group 1))) }}
2023-08-25 21:37:37 +00:00
* Weather for Hamburg, DE: Few clouds with a temperature of 22 C (71.6 F). [...]
2023-07-15 21:54:22 +00:00
```
2023-09-03 10:25:31 +00:00
### `usernameForID`
Returns the current login name of an user-id
Syntax: `usernameForID <user-id>`
Example:
```
# {{ usernameForID "12826" }}
* twitch
```
2023-08-14 13:44:23 +00:00
### `variable`
2021-11-19 21:53:30 +00:00
Returns the variable value or default in case it is empty
Syntax: `variable <name> [default]`
Example:
```
# {{ variable "foo" "fallback" }} - {{ variable "unsetvar" "fallback" }}
2023-08-25 21:37:37 +00:00
* test - fallback
2021-11-19 21:53:30 +00:00
```