Using telegram
Using Telegram for receiving function results
You can use a Telegram bot to receive the results of your SendBlocks functions. This is useful if you want to receive notifications on your phone or computer.
In order to achieve this, you will need to create a Telegram bot. You can follow this guide to create a bot. After creating the bot you will have a token
which you will use to send messages from your bot to any chat.
After creating the bot, you will need to get the chat_id
of the chat you want to send the results to. Start a chat with your bot and then retrieve the chat_id
by following these instructions.
Once you have your token
and chat_id
, you are ready to set up your function!
First, create a webhook with the following URL:
https://api.telegram.org/bot<token>/sendMessage
Where <token>
is the token of your bot (the secret
parameter you are asked to provide when creating the webhook is not used by Telegram and can be set to anything).
Then, when you create a function, you will need to set the webhook_id
field to the webhook_id
you just created. You will also need to set the should_send_std_streams
to false. Finally, in order to send the result of your function from your bot, your function will need to return an object with the following structure:
export function triggerHandler(context, data) {
// Your function code here
// message = ...
return {chat_id: <chat_id>, text: message};
};
Where <chat_id>
is the chat_id
of the chat you want to send the message to (the one you retrieved previously). Please note that the chat_id
needs be a string representation of an integer (eg. "123"), which can also be a negative number in the case of a group chat.
An example curl command to create your function will be along the lines of:
curl --request POST \
--url https://api.sendblocks.io/api/v1/functions \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"chain_id": "...",
"trigger": {
...
},
"should_send_std_streams": false,
"webhook_id": <webhook_id>,
"function_name": ...,
"function_code": ...,
}
'
You can use the invoke API to easily test your function and confirm that your new bot is handling the messages correctly.
Updated 29 days ago