Skip to content

Instantly share code, notes, and snippets.

@peted70

peted70/bot-impl Secret

Created June 17, 2016 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peted70/d54c6ac52eadc7156e2fe4055f373b06 to your computer and use it in GitHub Desktop.
Save peted70/d54c6ac52eadc7156e2fe4055f373b06 to your computer and use it in GitHub Desktop.
var data = await ParseUserInput(message.Text);
if (data.intents.Length <= 0 || data.entities.Length <= 0)
{
return message.CreateReplyMessage("I don't have enough information to understand the question - please try again...");
}
var topIntent = data.intents[0].intent;
switch (topIntent)
{
case "SummariseActivity":
var entityStr = data.entities.FirstOrDefault(e => e.type == "ActivityType").entity;
// This could be either date, time or duration..
var entityTime = data.entities.FirstOrDefault(e =>
e.type == "builtin.datetime.time" ||
e.type == "builtin.datetime.duration" ||
e.type == "builtin.datetime.date");
ParseResult<Period> res = null;
var entity = data.entities[0].entity;
if (entityTime.type == "builtin.datetime.duration")
{
res = PeriodPattern.NormalizingIsoPattern.Parse(entityTime.resolution.duration);
// Now call the relevant Microsoft Health API and respond to the user...
var st = SystemClock.Instance.GetCurrentInstant().InUtc().LocalDateTime - res.Value;
DateTime start = st.ToDateTimeUnspecified();
DateTime end = DateTime.Now;
var res2 = await GetActivity(token, entityStr, start, end);
var sleep = JsonConvert.DeserializeObject<Sleep>(res2);
// create a textual summary of sleep in that period...
int num = sleep.itemCount;
if (num <= 0)
{
prompt = "You didn't track any sleep";
break;
}
var total = sleep.sleepActivities.Sum((a) =>
{
if (a.sleepDuration != null)
{
var dur = PeriodPattern.NormalizingIsoPattern.Parse(a.sleepDuration);
return dur.Value.ToDuration().Ticks;
}
else
return 0;
});
var av = total / num;
var sleepSpan = TimeSpan.FromTicks((long)av);
var totalSpan = TimeSpan.FromTicks(total);
var avSleepStr = $"{sleepSpan.ToString(@"%h")} hrs {sleepSpan.ToString(@"%m")} mins";
var totalSleepStr = $"{totalSpan.ToString(@"%d")} days {totalSpan.ToString(@"%h")} hrs {totalSpan.ToString(@"%m")} mins";
prompt = $"You have tracked {num} sleeps - average sleep per night {avSleepStr} for a total of {totalSleepStr}";
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment