Skip to content

Instantly share code, notes, and snippets.

@MarcL
Created November 23, 2020 21:13
Show Gist options
  • Save MarcL/924db6f568c5be7b2c8c493d4cc62382 to your computer and use it in GitHub Desktop.
Save MarcL/924db6f568c5be7b2c8c493d4cc62382 to your computer and use it in GitHub Desktop.
Code to send a Chatfuel message
// New way to import my library
const chatfuelBroadcast = require('chatfuel-broadcast');
app.post('/broadcast-to-chatfuel', (request, response) => {
const { body } = request;
const { userId } = request.body;
const botId = '<your-bot-id>';
const token = '<your-token>';
const options = {
botId,
token,
userId,
messageTag: 'UPDATE',
blockName,
attributes: {
...body
}
};
return chatfuelBroadcast(options)
.then(() => {
response.json({
success: true,
});
})
.catch(error => {
response.json({
success: false,
error: error.toString()
});
});
});
const chatfuelBroadcast = require('chatfuel-broadcast').default;
app.post('/broadcast-to-chatfuel', (request, response) => {
console.log(request.body);
const botID = '<your-bot-id>';
const chatfuelToken = '<your-token>';
const { userId } = request.body;
const blockName = 'WebviewResponse';
const broadcastApiUrl = `https://api.chatfuel.com/bots/${botID}/users/${userId}/send`;
const query = {
chatfuel_token: chatfuelToken,
chatfuel_message_tag: 'ACCOUNT_UPDATE',
chatfuel_block_name: blockName,
...request.body,
};
const chatfuelApiUrl = url.format({
pathname: broadcastApiUrl,
query,
});
console.log(chatfuelApiUrl);
const options = {
url: chatfuelApiUrl,
headers: {
'Content-Type': 'application/json',
},
};
requestPromise.post(options)
.then(() => {
response.json({});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment