Get alerts from Ko-Fi via JavaScript
Use your favourite package manager, idk
npm install @ntf/kofi-alerts
yarn add @ntf/kofi-alerts
pnpm install @ntf/kofi-alerts
This library can be used in CommonJS
and ESModule
environments
const {...} = require("@ntf/kofi-alerts");
import {...} from "@ntf/kofi-alerts";
You need:
- the user key
- the page id
You can get the user key by visiting the Stream Alert Overlay section and then extract it from the URL that is provided (https://ko-fi.com/streamalerts/overlay/<user-key>
). On that same page you also have to set Alert Text
to:
Got {amount} from {from_name}!
You can get the page id by visiting the Connect to Zapier section and copy the value in Page Id
store the required information somewhere safe and pass it to the constructor of the class KoFiAlertsClient
:
const userKey = "<your-user-key>"
const pageId = "<your-page-id>"
const client = new KoFiAlertsClient({
userKey: userKey,
pageId: pageId,
// optional, see more on @microsoft/signalr type IHttpConnectionOptions
logger: ...
})
now use the exposed method connect
to connect to the servers. Keep in mind it can throw exceptions here
await client.connect()
when the connection has been enstablished, you can set a callback function on onalert
to get the alert
client.onalert = (alert) => {
console.info(alert)
}
The alert object has a type
property that can be:
The donation alert contains information about a donation that has been made at that moment
name
: The name of the donoramount
: The amount of money the doner has donated, this value is in the currency of the profiletts
: The text-to-speech message if it existsimage
: The image of the donor? not sure, might be HTML tooraw
: The raw message which is a HTML code snippet
The goal alert contains one property called goal
with the following properties:
Title
: The name of the goalGoalAmount
: The amount of money the goal has already receivedCurrency
: The currency the goal usesShowGoal
: Is the goal visibleProgressPercentage
: The percentage of the goal from0...100
as number
The activities alert contains just like the goal alert one property called activity
with the following properties:
AlertTimestamp
: UNIX timestamp when the alert happend?IsTestActivity
: Is this a test activity?TransactionId
: The UUID of the transactionTimestamp
: UNIX timestamp of when the transaction happend?UserName
: The name of the userAmount
: The amount of money that was used for the transaction. For some odd reason this is a string, not a numberCurrency
: The currency that was used for the transactionTwitchUsername
: The user's Twitch username. This is""
if not providedMessage
: The message of the user. This is""
if not providedIsMessagePublic
: Is this a public message that is viewable by anyone?TransactionType
: The type of transaction
if TransactionType
is Shop item
or Commission
then the following properties are valid:
ShopItemName
: The name of the item in the shop or commission
if TransactionType
is Membership
then the following properties are valid:
MembershipTierName
: The name of the membership tier
Keep in mind that this transaction is always fired when a user pays for the membership monthly so you would get this every month
There's also a TransactionType
called Donation
but it has no extra properties because it uses the Message
property only
Sometimes you might get this alert but it does not contain anything important, you only receive it when you change something in the stream donation/goal overlay options
When you call connect
on KoFiAlertsClient
the following things happen:
- a
GET
request is made onhttps://ko-fi.com/api/streamalerts/negotiation-token
withuserKey=<your-user-key>
and_=<current-UNIX-timestamp>
as URL parameters and you should get a JSON object returned withtoken
as a property - a
POST
request is made onhttps://sa-functions.ko-fi.com/api/negotiate
withnegotationToken=<the-token-from-above>
,pageId=<your-page-id>
andtimestamp=<YYYY_MM_DD_HH_MM_SS_MS>
as URL parameters and you should get a JSON object returned withurl
andaccessToken
as properties - from the
url
andaccessToken
we create a SignalR client and from there we can listen to various events:newStreamAlert
: A donation was receivedupdateGoalOverlay
: The goal has changed its stateupdateAlertActivity
: A activity was received
Q: What's the difference between donation
and activities
with TransactionType
Donation
?
A: Both alerts expose different kind of properties more or less but some are the same, it's up to you which one you choose
Just like any Open Source Project this has a License, the MIT License