Sending actions in events
Actions are objects that make events render buttons in the UI. Pressing these buttons will send a webhook to your server.
Here’s a simple example:
const e = {
name: "a critical event",
avatar: "🤖",
actions : [
{
url : 'https://webhook.site',
buttonText : 'Press me'
}
]
};
CopyYou can also have static buttons which simply open up a webpage. Here’s another example:
const e = {
name: "a critical event",
avatar: "🤖",
actions : [
{
url : 'https://stripe.com',
external : true,
buttonText : 'Press me'
}
]
};
CopyYou can pass Actions as a json array. Each object in the array has these params:
A valid url, eg https://stripe.com, http://server.io, etc.
Set the text of the button, eg: Send email to customer, Activate user, Open in stripe, etc.
A unique string to identify the action. Use this on your server to differentiate between different actions. For actions marked as external, no key is needed.
If set, opens the url in a external tab. Show a link icon in the button and behaves like a anchor tag basically.
Action cannot be run after in ‘expireIn’ minutes. Defaults to 10080(7 days). Max value cannot be more than 43200. We recommend this value to be as small as reasonably possible.
Defaults to null. This can be a string, or a object. This will be passed to the server when this action is triggered.
const e = {
name: "some event",
avatar: "🤖",
type : "rows",
actions : [
{
url : 'https://yourserver.com/api/someroute',
buttonText : 'Press me',
key : "press_me",
external : false,
expireIn : 10080,
meta : {
name : "Shash",
id : 1,
}
},
]
};
CopyHow do actions work?
When pressed, our server will send a POST request to the url specified. It will wait for 10 seconds and give up after that. If the server sends back a 200 or a 201 response status, the actions is marked as complete.
// Sample nodejs/fastify code to catch actions
const Fastify = require('fastify');
const fastify = Fastify();
fastify.register(require('fastify-formbody'));
fastify.post('/ops/action', async (request, reply) => {
try {
// Access the parsed body
const { actionType, data } = request.body;
// Your logic here
console.log('Received action:', actionType);
console.log('Data:', data);
// Respond to the client
return { status: 'success', received: { actionType, data } };
} catch (error) {
// Handle any errors
console.error('Error processing request:', error);
return reply.status(500).send({ status: 'error', message: 'Internal Server Error' });
}
});
// Start the server
const start = async () => {
try {
await fastify.listen(3000);
console.log('Server listening on http://localhost:3000');
} catch (err) {
console.error(err);
process.exit(1);
}
};
start();
CopyActions are great for triggering atomic events. For instance, you can use actions to ban users, or delete a user record. There are multiple safeguards in place to make sure you don’t trigger atomic actions accidently.
Here’s a quick setup for banning users.
const e = {
name: "some event",
avatar: "🤖",
type : "rows",
actions : [
{
url : 'https://yourserver.com/api/someroute',
buttonText : 'Ban user',
key : 'ban_user',
expireIn : 120
},
]
};
CopyNote the really small expireIn time. This will ensure we cannot run our trigger after 2 hours from now(where it won’t be effective).