Skip to content

Instantly share code, notes, and snippets.

@uphotelagency
Created March 24, 2020 14:54
Show Gist options
  • Save uphotelagency/f3cbe8eac4ddd41647aabea9ca2ca0a2 to your computer and use it in GitHub Desktop.
Save uphotelagency/f3cbe8eac4ddd41647aabea9ca2ca0a2 to your computer and use it in GitHub Desktop.
const path = require('path');
const restify = require('restify');
const eventSource = require('./transformers/eventSource');
const transformer = require('./transformers/transformer');
const utility = require('./botUtility');
// Import required bot services. See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');
// This bot's main dialog.
const { ProactiveBot } = require('./bot-main');
// Note: Ensure you have a .env file and include the MicrosoftAppId and MicrosoftAppPassword.
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log
// NOTE: In production environment, you should consider logging this to Azure
// application insights.
console.error(`\n [onTurnError]: ${ error }`);
// Send a message to the user
await context.sendActivity('\n [onTurnError]: ' + error);
};
// Create the main dialog.
const conversationReferences = {};
const bot = new ProactiveBot(conversationReferences);
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log(`\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator`);
console.log(`\nMicrosoft App ID for Bot Emulator: ${process.env.MicrosoftAppId}`);
console.log(`\nMicrosoft App Password for Bot Emulator: ${process.env.MicrosoftAppPassword}`);
});
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
// route to main dialog.
await bot.run(turnContext);
});
});
//Listen for incoming notifications and send them through to all conversations the bot is involved in.
server.post('/api/notify', async(req,res) =>{
for (const conversationReference of Object.values(conversationReferences)) {
adapter.continueConversation(conversationReference, async turnContext => {
//await turnContext.sendActivity(req.body);
if(req.body.source === "aws.codebuild"){
if(req.body.detail["build-status"] === "FAILED"){
const data = transformer.codeBuildTransform(req.body);
await turnContext.sendActivity(`Stop the Clocks, we're killing time because something failed in the build: \n\n${data}`);
}else{
return;
}
}
else if(req.body.source === "aws.codepipeline"){
if(!(req.body.detail.stage.includes("Build") || req.body.detail.stage.includes("Approve") || req.body.detail.stage.includes("Approval"))){
const data = transformer.codePipelineFailureTransform(req.body);
await turnContext.sendActivity(`Woops, looks like a Stage has failed in a Pipeline: \n\n${data}`);
}else{
return;
}
}
else{
return;
//const data = transformer.transform(req.body);
await turnContext.sendActivity("This is the data I received From " + eventSource.sourceNamer(req.body) + ": "+"\n\n"+JSON.stringify(req.body,null,4));
}
});
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
res.end();
})
server.get('/api/timer', async(req,res) => {
for(const conversationReference of Object.values(conversationReferences)){
adapter.continueConversation(conversationReference, async turnContext => {
let joke;
await utility.joke().then(function (response){
if(response.data.type === "single"){
joke = `${response.data.joke}`;
}else{
joke = `
${response.data.setup}
${response.data.delivery}
`;
}
});
await turnContext.sendActivity(`Good Morning Full Stackers! Here's your joke of the day:
${joke}`);
});
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Joke has been sent.</h1></body></html>');
res.end();
})
exports.handler = server;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment