protected async Task <Tuple <bool, List <MeetingRoom> > > BookRoom(MeetingRoomSuggestionConstrain constrain)
        {
            List <MeetingRoom> suggestions = new List <MeetingRoom>();

            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {this._credential.ToString()}");

                    var meetingResponse = await client.PostAsync("https://graph.microsoft.com/beta/me/events", new StringContent(GetBookRoomBody(constrain)));

                    if (meetingResponse.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        Trace.TraceError($"BookRoom: StatusCode={meetingResponse.StatusCode}");
                        return(new Tuple <bool, List <MeetingRoom> >(false, null));
                    }


                    return(new Tuple <bool, List <MeetingRoom> >(true, suggestions));
                }
            }
            catch
            {
                throw;
            }
        }
        protected async Task <Tuple <bool, List <MeetingRoom> > > FindRooms(MeetingRoomSuggestionConstrain constrain)
        {
            var suggestions = new List <MeetingRoom>();

            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer {this._credential.ToString()}");

                    var meetingResponse = await client.PostAsync("https://graph.microsoft.com/beta/me/findMeetingTimes", new StringContent(String.Empty));

                    if (meetingResponse.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        Trace.TraceError($"GetMeetingRoomSuggestions: StatusCode={meetingResponse.StatusCode}");
                        return(new Tuple <bool, List <MeetingRoom> >(false, null));
                    }

                    dynamic meetingTimes = JObject.Parse(await meetingResponse.Content.ReadAsStringAsync());

                    foreach (var item in meetingTimes.meetingTimeSuggestions[0].locations)
                    {
                        // Add only locations with an email address -> meeting rooms
                        if (!String.IsNullOrEmpty(item.locationEmailAddress.ToString()))
                        {
                            suggestions.Add(new MeetingRoom()
                            {
                                DisplayName = item.displayName,
                                Email       = item.locationEmailAddress
                            });
                        }
                    }

                    return(new Tuple <bool, List <MeetingRoom> >(true, suggestions));
                }
            }
            catch (Exception e)
            {
                Trace.TraceError($"GetMeetingRoomSuggestions: Exception={e.Message}.");
                return(new Tuple <bool, List <MeetingRoom> >(false, null));
            }
        }
 public override Task <Tuple <bool, List <MeetingRoom> > > FindAvailableMeetingRooms(MeetingRoomSuggestionConstrain constrain)
 {
     if (constrain.MeetingRoomIds?.Count() == 1)
     {
         //booked
         return(Task.FromResult <Tuple <bool, List <MeetingRoom> > >(new Tuple <bool, List <MeetingRoom> >(true, new List <MeetingRoom>
         {
             new MeetingRoom
             {
                 DisplayName = constrain.MeetingRoomIds[0],
                 Email = "*****@*****.**"
             }
         })));
     }
     else
     {
         return(Task.FromResult <Tuple <bool, List <MeetingRoom> > >(new Tuple <bool, List <MeetingRoom> >(false, new List <MeetingRoom>
         {
             new MeetingRoom
             {
                 DisplayName = "A001",
                 Email = "*****@*****.**"
             },
             new MeetingRoom
             {
                 DisplayName = "A002",
                 Email = "*****@*****.**"
             },
             new MeetingRoom
             {
                 DisplayName = "A003",
                 Email = "*****@*****.**"
             },
         })));
     }
 }
 public Task <bool> BookMeetingRoom(MeetingRoomSuggestionConstrain constrain)
 {
     return(Task.FromResult <bool>(true));
 }
        public override async Task <Tuple <bool, List <MeetingRoom> > > FindAvailableMeetingRooms(MeetingRoomSuggestionConstrain constrain)
        {
            FindMeetingRoomParam param = new FindMeetingRoomParam();

            param.Account   = constrain.MeetingOrganizer;
            param.Capacity  = constrain.Size;
            param.Period    = (int)(constrain.End - constrain.Start).TotalMinutes;
            param.RoomName  = constrain.MeetingRoomIds?.First() ?? string.Empty;
            param.StartTime = constrain.Start;

            _restUrl = ConfigurationManager.AppSettings["MeetingRoomApiUrl"];

            var json = JsonConvert.SerializeObject(param);

            Trace.TraceInformation($"[Invoke REST API]{json}");
            using (var resp = await _client.PostAsync(_restUrl, new StringContent(json, Encoding.UTF8, "application/json")))
            {
                var body = await resp.Content.ReadAsStringAsync();

                if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var rooms = JsonConvert.DeserializeObject <FindMeetingRoomResult>(body);
                    if (rooms.Result == true)
                    {
                        //Book successfully
                        return(new Tuple <bool, List <MeetingRoom> >(true, null));
                    }
                    else
                    {
                        //No room available, but we have suggestions for you.
                        var results = rooms?.Recommended?.Select(r => new MeetingRoom
                        {
                            DisplayName = r,
                            Email       = string.Empty
                        })?.ToList();
                        return(new Tuple <bool, List <MeetingRoom> >(false, results));
                    }
                }
                else
                {
                    //API failed
                    return(new Tuple <bool, List <MeetingRoom> >(false, null));
                }
            }
        }
Exemplo n.º 6
0
 public abstract Task <Tuple <bool, List <MeetingRoom> > > FindAvailableMeetingRooms(MeetingRoomSuggestionConstrain constrain);
        private string GetBookRoomBody(MeetingRoomSuggestionConstrain constrain)
        {
            var room = new
            {
                subject = $"Booked by Meeting room bot for {constrain.MeetingOrganizer}",
                body    = new
                {
                    contentType = "HTML",
                    content     = $"Booked by Meeting room bot for {constrain.MeetingOrganizer}"
                },
                start = new
                {
                    dateTime = constrain.Start.ToString("yyyy-MM-ddTHH:mm:ss") + "Z" + constrain.TimezoneOffset.Value.Hours.ToString("00") + "00"
                },
                end = new
                {
                    dateTime = constrain.End.ToString("yyyy-MM-ddTHH:mm:ss") + "Z" + constrain.TimezoneOffset.Value.Hours.ToString("00") + "00"
                },
                location = new
                {
                    displayName = constrain.MeetingRoomIds?[0]
                },
                attendees = new[]
                {
                    new
                    {
                        emailAddres = new
                        {
                            address = constrain.MeetingOrganizer
                        },
                        type = "required"
                    }
                }
            };

            return(JsonConvert.SerializeObject(room));

            /*
             * {
             * "subject": "Let's go for lunch",
             * "body": {
             * "contentType": "HTML",
             * "content": "Does late morning work for you?"
             * },
             * "start": {
             * "dateTime": "2017-04-15T12:00:00",
             * "timeZone": "Pacific Standard Time"
             * },
             * "end": {
             * "dateTime": "2017-04-15T14:00:00",
             * "timeZone": "Pacific Standard Time"
             * },
             * "location":{
             * "displayName":"Harry's Bar"
             * },
             * "attendees": [
             * {
             * "emailAddress": {
             * "address":"*****@*****.**",
             * "name": "Fanny Downs"
             * },
             * "type": "required"
             * }
             * ]
             * }
             *
             * */
        }
 public override async Task <Tuple <bool, List <MeetingRoom> > > FindAvailableMeetingRooms(MeetingRoomSuggestionConstrain constrain)
 {
     if (constrain.MeetingRoomIds?.Count() == 1)
     {
         return(await FindRooms(constrain));
     }
     else
     {
         //book room
         return(await BookRoom(constrain));
     }
 }