| /* HELPER FUNCTIONS */ | |
| function selectRandomFact() { | |
| const FACTS_ARRAY = ['description', 'released', 'global', 'regions', | |
| 'geographies', 'platforms', 'categories', 'products', 'cognitive', | |
| 'compliance', 'first', 'certifications', 'competition', 'functions']; | |
| return FACTS_ARRAY[Math.floor(Math.random() * FACTS_ARRAY.length)]; | |
| } | |
| function buildFactResponse(factToQuery, callback) { | |
| if (factToQuery.toString().trim() === 'random') { | |
| factToQuery = selectRandomFact(); | |
| } | |
| mongoClient.connect(COSMOS_DB_CONN_STR, function (err, client) { | |
| const db = client.db(DB_COLLECTION); | |
| findFact(db, factToQuery, function (document) { | |
| client.close(); | |
| if (!document) { | |
| console.log(`No document returned for value of ${factToQuery}?`); | |
| } | |
| return callback(document); | |
| }); | |
| }); | |
| } | |
| function createHeroCard(session, botResponse) { | |
| return new builder.HeroCard(session) | |
| .title('Azure Tech Facts') | |
| .subtitle(botResponse.title) | |
| .text(botResponse.response) | |
| .images([ | |
| builder.CardImage.create(session, `${ICON_STORAGE_URL}/${botResponse.image}`) | |
| ]) | |
| .buttons([ | |
| builder.CardAction.openUrl(session, 'https://azure.microsoft.com', 'Learn more...') | |
| ]); | |
| } | |
| function createThumbnailCard(session, botResponse) { | |
| return new builder.ThumbnailCard(session) | |
| .title('Azure Tech Facts') | |
| .subtitle(botResponse.title) | |
| .text(botResponse.response) | |
| .images([ | |
| builder.CardImage.create(session, `${ICON_STORAGE_URL}/${botResponse.image}`) | |
| ]) | |
| .buttons([ | |
| builder.CardAction.openUrl(session, 'https://azure.microsoft.com', 'Learn more...') | |
| ]); | |
| } | |
| function findFact(db, factToQuery, callback) { | |
| console.log(`fact to query: ${factToQuery}`); | |
| db.collection(DB_COLLECTION).findOne({"fact": factToQuery}) | |
| .then(function (document) { | |
| if (!document) { | |
| console.log(`No document found for fact '${factToQuery}'`); | |
| } | |
| console.log(`Document found: ${JSON.stringify(document)}`); | |
| return callback(document); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment