Skip to content

Instantly share code, notes, and snippets.

View jazzedge's full-sized avatar

Rocco Labellarte jazzedge

View GitHub Profile
@jazzedge
jazzedge / gist:20b9bf83536cfe20d31a692d99feaa09
Created July 12, 2017 05:51
Bot - Morning, Afternoon, Evening function
// This function returns a string indicating if it is morning, afternoon or evening.
function amPm () {
const date = new Date().getHours();
console.log ('Date:', date);
var period = 'evening';
if (date < 12) {
period = "morning";
} else {
if (date < 18){
period = "afternoon";
@jazzedge
jazzedge / gist:df956088af78141f5068c746aaab4103
Last active August 3, 2017 04:47
Bot - Localization #3 load JSON file
// https://github.com/Microsoft/BotBuilder-Samples/blob/master/Node/demo-ContosoFlowers/README.md
// The SDK also provides a way to configure the default bot's locale:
// Set default locale
bot.set('localizerSettings', {
botLocalePath: './bot/locale',
defaultLocale: 'en'
});
var bot = new builder.UniversalBot(connector, function (session) {
var cards = getCardsAttachments();
// create reply with Carousel AttachmentLayout
var reply = new builder.Message(session)
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(cards);
session.send(reply);
});
byte[] imagedata = {image}
var image64 = "data:image/jpeg;base64," + Convert.ToBase64String(imagedata);
reply.Attachments.Add(new Attachment()
{
ContentUrl = image64,
ContentType = "image/jpeg",
});
@jazzedge
jazzedge / gist:c230182b306b0d7177e7af58a22d4cdc
Created July 23, 2017 17:02
Bot - Load image from Blob storage
Display BASE64 file - https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html
<img src=' ... '/>
static void BlobUrl()
{
var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var cloudBlobClient = account.CreateCloudBlobClient();
var container = cloudBlobClient.GetContainerReference("container-name");
var blob = container.GetBlockBlobReference("image.png");
//Vision API test
// Build the url we'll be calling to get top news
var url = "https://westeurope.api.cognitive.microsoft.com/vision/v1.0/describe/";
// Build options for the request
session.send(attachment.contentUrl + '/' + attachment.name);
var options = {
method: 'POST', // thie API call is a post request
uri: url,
headers: {
@jazzedge
jazzedge / gist:8c7f547a824e3b23727f387cad6653ec
Created July 24, 2017 16:18
Bot - Initiate action when new conversation begins
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.send(new builder.Message()
.address(message.address)
.text("Hello! I'm a bot."));
}
});
}
session.sendTyping();
bot.use(builder.Middleware.sendTyping()); //Bug: doesn't seem to work: https://github.com/Microsoft/BotBuilder/issues/1278
@jazzedge
jazzedge / gist:477176847ba6f51242634d66e40134da
Created July 27, 2017 04:48
Bot - Node JS get max depth of a JSON structure
// ----------------------------------------------------------------------------------------------------
// Calculates the maximum depth of a JSON object. You must pass the string before converting it to JSON!
function getDepth(obj) {
var depth = 0;
var maxDepth = 0;
var i = 0;
while (i < obj.length)
{
switch (obj.substr(i,1)) {
case "{":
@jazzedge
jazzedge / gist:2105fcd60afdd159e8fdf6c07a97bb0a
Last active July 28, 2017 05:53
Bot - Localization #2 - Set Locale
// You can either set your preferred Locale like this (default is en if you dont do anything)
session.preferredLocale("en");
// Allow user to set locale
if (!session.userData['BotBuilder.Data.PreferredLocale']) {
session.beginDialog('/localePicker');
}
localePicker.js
let builder = require("botbuilder")