Skip to content

Instantly share code, notes, and snippets.

@eric-mathison
Created January 11, 2018 10:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric-mathison/0371b024efd14878f38da21c9cd3001a to your computer and use it in GitHub Desktop.
Save eric-mathison/0371b024efd14878f38da21c9cd3001a to your computer and use it in GitHub Desktop.
Chatfuel Template Clone Api - NodeJS
BEARER=<Insert your Bearer Auth Token Here>
{
"//1": "describes your app and its dependencies",
"//2": "https://docs.npmjs.com/files/package.json",
"//3": "updating this file will download and update your packages",
"name": "chatfuel-template-glitch-app",
"version": "1.0.0",
"description": "Chatfuel Dashboard Template API",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.2",
"bluebird": "^3.5.1",
"request": "^2.83.0"
},
"engines": {
"node": "8.x"
},
"repository": {
"url": "https://glitch.com/edit/#!/welcome-project"
},
"license": "MIT",
"keywords": [
"node",
"glitch",
"express"
]
}
const express = require('express');
const Promise = require('bluebird');
const Request = Promise.promisify(require('request'));
var app = express();
// handle error handling
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('Something broke!')
});
app.get("/bot", (req, res) => {
var botID = req.query.botID;
var bearerID = process.env.BEARER;
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var randomInt = getRandomInt(999);
function callBot(host, method, title) {
var options = {
uri: host,
method: method,
headers: {
Bearer: bearerID,
'Content-Type': 'application/json'
},
body: {title},
json: true
};
return Request(options);
};
var botName, new_botID;
var url1 = `https://dashboard.chatfuel.com/api/bots/${botID}`;
callBot(url1, 'GET', '').then(function(result1) {
botName = result1.body.result.title;
var url2 = `https://dashboard.chatfuel.com/api/bots/`;
var body2 = `${botName} ${randomInt}`;
return callBot(url2, 'POST', body2);
}).then(function(result2) {
new_botID = result2.body.result.id;
var url3 = `https://dashboard.chatfuel.com/api/bots/${new_botID}/clone_from/${botID}`;
return callBot(url3, 'POST', '');
}).then(function(result3) {
var url4 = `https://dashboard.chatfuel.com/api/bots/${new_botID}/invite`;
return callBot(url4, 'GET', '');
}).then(function(result4) {
var inviteUrl = result4.body.result;
res.redirect(inviteUrl);
})
.catch((err) => {
res.status(500).send('Error 447: There was a problem generating your link. Please inform us of this error. Sorry for the inconvenience.');
});
});
app.use(function (req, res, next) {
res.status(404).send("Sorry can't find that!")
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
@rmatawi
Copy link

rmatawi commented Jul 5, 2018

Hi Eric, I just downloaded your script. Will take it for a spin and share my feedback.

@AMASHALA
Copy link

i have test the same but i have question for you @rmatawi @eric-mathison am getting error while redirecting at the last function that saying
{"code":500,"message":"There was an error processing your request. It has been logged (ID 3c4c7eea4b99299c)."}
could someone inform me where is the problem to fix it ?

@AMASHALA
Copy link

i solved if anyone needs the worked code let me know 👍 💯 😄

@betti1965
Copy link

Hi @AMASHALA I am interested, how can i get your working code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment