public void CreateActivity(Activity model) { // Insert if (!ModelState.IsValid) { Context.Response.StatusCode = 400; } else { _dbContext.Activities.Add(model); _dbContext.SaveChanges(); string url = Url.RouteUrl("GetByIdRoute", new { id = model.Id }, Request.Scheme, Request.Host.ToUriComponent()); Context.Response.StatusCode = 201; Context.Response.Headers["Location"] = url; } }
public async Task<ActionResult> Inbox(RallyNowDbContext dbContext) { string token = Context.Session.GetString("access_token"); string email = Context.Session.GetString("user_email"); if (string.IsNullOrEmpty(token)) { // If there's no token in the session, redirect to Home return Redirect("/"); } try { OutlookServicesClient client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v1.0"), async () => { return token; }); client.Context.SendingRequest2 += (sender, e) => InsertXAnchorMailboxHeader(sender, e, email); var mailResults = await client.Me.Messages .OrderByDescending(m => m.DateTimeReceived) .Take(20) .Select(m => new Emails { Id = m.Id, Subject = m.Subject, DateTimeSent = m.DateTimeReceived.Value.LocalDateTime, From = m.From.EmailAddress.Name, Body = m.BodyPreview, }) .ExecuteAsync(); foreach (var mailResult in mailResults.CurrentPage) { var existingEmail = _dbContext.Emails .SingleOrDefault(c => c.Id == mailResult.Id); if (existingEmail == null) { var activity = new Activity { Date = mailResult.DateTimeSent, MessageSource = "Email", Message = $"Subject: {mailResult.Subject} \r\n Body: {mailResult.Body}", }; WebClient webClient = new WebClient(); var response = webClient.DownloadString( "https://api.projectoxford.ai/luis/v1/application?id=f375bec7-02a2-4119-ab65-ca89ea673b22&subscription-key=11ea75021c944b31bf1a7d7c465175c5&q=" + HttpUtility.UrlEncode(mailResult.Body)); JObject jResults = JObject.Parse(response); try { var intent = jResults["intents"][0]["intent"].Value<string>(); if (intent != "builtin.intent.none") { var entities = jResults["entities"]; foreach (var entity in entities) { var type = entity["type"].Value<string>(); var resolution = entity["resolution"].Value<string>(); } } activity.Action = intent; } catch { } _dbContext.Emails.Add(mailResult); _dbContext.Activities.Add(activity); } } _dbContext.SaveChanges(); return Ok(); } catch (AdalException ex) { return Content(string.Format("ERROR retrieving messages: {0}", ex.Message)); } }
public async Task<ActionResult> Calendar() { string token = Context.Session.GetString("access_token"); string email = Context.Session.GetString("user_email"); ; if (string.IsNullOrEmpty(token)) { // If there's no token in the session, redirect to Home return Redirect("/"); } try { OutlookServicesClient client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v1.0"), async () => { // Since we have it locally from the Session, just return it here. return token; }); client.Context.SendingRequest2 += (sender, e) => InsertXAnchorMailboxHeader(sender, e, email); var eventResults = await client.Me.Events .OrderBy(e => e.Start) .Take(20) .Select(e => new CalendarEvent { Id = e.iCalUId, Body = e.Body.Content, Location = e.Location.DisplayName, Subject = e.Subject, Start = e.Start.Value.LocalDateTime, End = e.End.Value.LocalDateTime, }) .ExecuteAsync(); foreach (var eventResult in eventResults.CurrentPage) { var existingEvent = _dbContext.CalendarEvents .SingleOrDefault(c => c.Id == eventResult.Id); if (existingEvent == null) { var activity = new Activity { Date = eventResult.Start, MessageSource = "Calendar", Message = $"{eventResult.Subject} between {eventResult.Start} to {eventResult.End} at {eventResult.Location}.", }; _dbContext.CalendarEvents.Add(eventResult); _dbContext.Activities.Add(activity); } } _dbContext.SaveChanges(); return Ok(); } catch (AdalException ex) { return Content(string.Format("ERROR retrieving events: {0}", ex.Message)); } }