Skip to main content

Using Server Webhooks

What are Server Webhooks?

These Webhooks are used to get access to things that are happening with your extension. Enables, Disables, Removals, Adds.

Websocket Webhooks

Webhook URL - https://server.chatrpg.com/pubsub/subscribe

  • Enable Extension
  • Disable Extension
  • Add Extension
  • Remove Extension

Body - Must be included

  • url - The endpoint URL where you want to receive webhook notifications.
  • clientId - Your Client ID for your extension
  • clientSecret - Your Client Secret for your extension
  • topics - An Object of the topics you want to subscribe to, of type boolean

Topics - You must include at least 1 - Failure to do so will unsubscribe your URL:

  • enableExtension - Gets when a streamer enables your extension
  • addExtension - Gets when a streamer adds your extension - Also enables your extension
  • removeExtension - Gets when a streamer removes your extension - Also disables your extension
  • disableExtension - Gets when a streamer disables your extension

How can I connect to Server Extension Webhooks?

Subscribe to Server Webhook

https://server.chatrpg.com/pubsub/subscribe

You only need to include the topics you want, and set them to true.

const subscribeToServerWebhooks = async (): Promise<void> => {
const subscriptionUrl = `https://server.chatrpg.com/pubsub/subscribe`; //The ChatRPG pubsub URL

console.log(`Subscribing streamers to pubsub at ${subscriptionUrl}`);

try {
const response = await axios.post(subscriptionUrl, {
url: `${process.env.THIS_APPLICATION_URL}/webhook`, //Your URL
clientId: process.env.CHATRPG_CLIENT,
clientSecret: process.env.CHATRPG_SECRET,
topics: { //List of topics you're interested in
enableExtension: true, //Alerts you when a user enables the extension - if set to true you will receive this topic
addExtension: false, //Alerts you when a user adds your extension - if set to false you remove this topic
}
});

if (response.status === 200) {
console.log(`Successfully subscribed to all necessary server webhooks.`);
} else {
console.log(`Failed to subscribe streamers. Status code: ${response.status}`);
}
} catch (error) {
console.error(`Error in subscribeStreamersToWebhooks: ${error.message}`);
}
};

Then you should process the webhook messages however you see fit.

You can discern which message is which by using the "type" field.

router.post('/', async (req, res) => {
try {
console.log(`Received payload: ${JSON.stringify(req.body)}`);

//Example Disable Payload: {"type":"disableExtension","streamer_chatrpg_id":"CRPG1COWSEP", "streamer_chatrpg_username":"Cowsep", "clientId":"client_6fbbf99b240a4fbfb3e712389b6b86f1"}
//Example Add Payload: {"type":"addExtension","streamer_chatrpg_id":"CRPG1COWSEP", "streamer_chatrpg_username":"Cowsep", "clientId":"client_6fbbf99b240a4fbfb3e712389b6b86f1"}

if (req.body.type === 'disableExtension') {
console.log(`Received payload ${req.body.streamer_chatrpg_id} just disabled his extension!!`);
}

res.status(200).send('Received Playload Successfully');
} catch (error) {
console.error('Server error:', error);
res.status(500).send('Server error');
}
});

How do I remove my Topic?

You can easily remove a topic by simply using this endpoint.

Remove Topic

https://server.chatrpg.com/pubsub/removetopic

const unsubscribeFromTopics = async (): Promise<void> => {
// URL of the ChatRPG pubsub system for removing streamer IDs
const subscriptionUrl = `https://server.chatrpg.com/pubsub/removetopic`;

console.log(`Attempting to unsubscribe from topics at ${subscriptionUrl}`);

const exampleTopics = ['enableExtension'];

try {
// Send a POST request to unsubscribe the streamers
const response = await axios.post(subscriptionUrl, {
url: `${process.env.THIS_APPLICATION_URL}/webhook`, // Endpoint on your server that you want to unsubscribe
topics: exampleTopics, //Topics you want to unsubscribe
clientId: process.env.CHATRPG_CLIENT, // Client ID for the ChatRPG service
clientSecret: process.env.CHATRPG_SECRET, // Secret for authentication
});

if (response.status === 200) {
console.log(`Successfully unsubscribed from that topic.`);
} else {
console.log(`Failed to unsubscribe streamers. Status code: ${response.status}`);
}
} catch (error) {
// Log any errors that occur during the HTTP request
console.error(`Error in unsubscribeStreamersFromWebhooks: ${error.message}`);
}
};

How do I remove myself from the Subscription?

URLs are removed automatically after 10 days but if you change any topics it will renew the expiration date 10 days out again.

If you want to remove it you can use this end point:

Unsubscribe from Webhook

https://server.chatrpg.com/pubsub/unsubscribe

const subscribeStreamersToWebhooks = async (streamerIds: string[]): Promise<void> => {
const subscriptionUrl = `https://ws.chatrpg.com/pubsub/subscribe`; //The ChatRPG pubsub URL

console.log(`Subscribing streamers to pubsub at ${subscriptionUrl}`);

try {
const response = await axios.post(subscriptionUrl, {
url: `${process.env.THIS_APPLICATION_URL}/webhook`, //Your URL
streamerIds: streamerIds, // Send array of IDs
clientId: process.env.CHATRPG_CLIENT,
clientSecret: process.env.CHATRPG_SECRET,
topics: { //List of topics you're interested in
chatMessages: true, //Alerts you when Chat Messages happen
socketConnections: false, //Alerts you when a User Connects or Disconnects
enableExtension: true, //Alerts you when a streamer enables extensions
disableExtension: true //Alerts you when a streamer disables the extension
}
});

if (response.status === 200) {
console.log(`Successfully subscribed streamers to all necessary webhooks.`);
} else {
console.log(`Failed to subscribe streamers. Status code: ${response.status}`);
}
} catch (error) {
console.error(`Error in subscribeStreamersToWebhooks: ${error.message}`);
}
};