Ejemplo n.º 1
0
 public static async Task <List <Request> > dataquery(cleandata data)
 {
     using (var DB = new travelrecordEntities())
     {
         var match = (from b in DB.Requests where (b.origin == data.origin || data.origin == "") && (b.destination == data.dest || data.dest == "") && (b.date1 == data.date || data.date == null) && (b.time1 == data.time.TimeOfDay || data.time == default(DateTime)) && (b.MAXNO > 0) select b).ToList();
         return(match);
     }
 }
Ejemplo n.º 2
0
        public static async Task <cleandata> Clean(LUIS data)
        {
            cleandata clean = new cleandata();

            try
            {
                clean.origin = data.entities.Where(b => b.type == "location::fromlocation").FirstOrDefault().entity;
            }
            catch {
                clean.origin = "";
            }
            try
            {
                clean.dest = data.entities.Where(b => b.type == "location::tolocation").FirstOrDefault().entity;
            }
            catch
            {
                clean.dest = "";
            }
            try
            {
                string str = data.entities.Where(b => (b.type == "builtin.datetime.date")).FirstOrDefault().resolution.date.Replace("XXXX", "2017");
                clean.date = Convert.ToDateTime(str);
            }
            catch
            {
                clean.date = null;
            }
            try
            {
                if (DateTime.TryParseExact(data.entities.Where(b => (b.type == "builtin.datetime.time")).FirstOrDefault().resolution.time.Substring(1), "HH", CultureInfo.InvariantCulture, DateTimeStyles.None, out clean.time))
                {
                }
                else if (DateTime.TryParseExact(data.entities.Where(b => (b.type == "builtin.datetime.time")).FirstOrDefault().resolution.time.Substring(1), "HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out clean.time))
                {
                }
                else if (DateTime.TryParseExact(data.entities.Where(b => (b.type == "builtin.datetime.time")).FirstOrDefault().resolution.time.Substring(1), "HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out clean.time))
                {
                }
                //clean.time = DateTime.ParseExact(data.entities.Where(b => (b.type == "builtin.datetime.time")).FirstOrDefault().resolution.time.Substring(1), "HH", CultureInfo.InvariantCulture);
            }
            catch
            {
                return(clean);
            }
            return(clean);
        }
Ejemplo n.º 3
0
        public static async Task <string> addquery(cleandata data, string username, string psid, string fbid)
        {
            using (var DB = new travelrecordEntities())
            {
                Request user = new Request();

                user.name        = username;
                user.origin      = data.origin.ToUpper();
                user.destination = data.dest.ToUpper();
                user.date1       = data.date;
                user.time1       = data.time.TimeOfDay;
                user.fbid        = fbid;
                user.psid        = psid;
                user.MAXNO       = data.noop;
                user.names       = username;
                DB.Requests.Add(user);
                await DB.SaveChangesAsync();
            }
            return("Request added.");
        }
Ejemplo n.º 4
0
        private static async Task <int> ReplyCreate(Activity activity, ConnectorClient connector)
        {
            var x = await GetEntityFromLUIS(activity.Text);

            if (activity.Text == "YES" || activity.Text == "NO")
            {
                string naam = "a";

                var    c       = JObject.Parse(activity.ChannelData.ToString());
                string s       = (String)c["message"]["quick_reply"]["payload"];
                var    results = new List <string>();
                //var subjectString = "My Name is #P_NAME# and \r\n I am #P_AGE# years old";
                Regex regexObj     = new Regex("_.+?_");
                Match matchResults = regexObj.Match(s);
                while (matchResults.Success)
                {
                    results.Add(matchResults.ToString().Replace("_", ""));
                    matchResults = matchResults.NextMatch();
                }
                int i;
                int.TryParse(results[1], out i);
                using (var DB = new travelrecordEntities())
                {
                    Request match = DB.Requests.Where(b => b.id == i).FirstOrDefault();
                    naam = match.name;
                }


                if (results[0] == "YES")
                {
                    string e = await GetUserName1(results[2]);

                    Request match;
                    using (var DB = new travelrecordEntities())
                    {
                        match = DB.Requests.Where(b => b.id == i).FirstOrDefault();
                    }
                    naam = match.name;
                    if (match != null)
                    {
                        match.names = match.names + ", " + e;
                        match.MAXNO--;
                    }
                    await JoinCard.show(activity, connector, match.names);

                    try
                    {
                        using (var db = new travelrecordEntities())
                        {
                            db.Entry(match).State = EntityState.Modified;
                            await db.SaveChangesAsync();
                        }
                        var botAccount = activity.Recipient;
                        var f          = await GetUserName1(results[2]);

                        var userAccount    = new ChannelAccount(name: f, id: results[2]);
                        var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);

                        IMessageActivity message = Activity.CreateMessageActivity();
                        message.From         = botAccount;
                        message.Recipient    = userAccount;
                        message.Conversation = new ConversationAccount(id: conversationId.Id);
                        message.Text         = String.Format("The join request you sent to {0} was accepted.", naam);
                        message.Locale       = "en-Us";
                        await connector.Conversations.SendToConversationAsync((Activity)message);
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                await JoinCard.show(activity, connector, validationError.ErrorMessage);

                                await JoinCard.show(activity, connector, validationError.PropertyName);
                            }
                        }
                    }
                    await JoinCard.show(activity, connector, String.Format("{0} was added to your pool.", e));
                }
                else
                {
                    try {
                        var botAccount = activity.Recipient;
                        var e          = await GetUserName1(results[2]);

                        var userAccount    = new ChannelAccount(name: e, id: results[2]);
                        var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);

                        IMessageActivity message = Activity.CreateMessageActivity();
                        message.From         = botAccount;
                        message.Recipient    = userAccount;
                        message.Conversation = new ConversationAccount(id: conversationId.Id);
                        message.Text         = String.Format("The join request you sent to {0} was not accepted.", naam);
                        message.Locale       = "en-Us";
                        await connector.Conversations.SendToConversationAsync((Activity)message);
                    }
                    catch (Exception ex)
                    {
                        await JoinCard.show(activity, connector, ex.Message);
                    }
                }
                return(1);
            }
            else if (x.topScoringIntent.intent == "Greeting")
            {
                string y = await GetUserName(activity);

                await JoinCard.show(activity, connector, "Hey " + y + "!");

                return(1);
            }
            else if (x.topScoringIntent.intent == "join")
            {
                var     c              = JObject.Parse(activity.ChannelData.ToString());
                Request request        = JsonConvert.DeserializeObject <Request>(c["postback"]["payload"] + "");
                var     botAccount     = activity.Recipient;
                var     userAccount    = new ChannelAccount(name: request.name, id: request.psid);
                var     conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);

                IMessageActivity message = Activity.CreateMessageActivity();
                message.From         = botAccount;
                message.Recipient    = userAccount;
                message.Conversation = new ConversationAccount(id: conversationId.Id);
                message.Text         = String.Format("{0} wants to join your carpool. Select 'YES' or 'NO'.", activity.From.Name);
                message.Locale       = "en-Us";
                List <CardAction> cardButtons = new List <CardAction>();
                CardAction        namebutton  = new CardAction()
                {
                    Value = "https://www.facebook.com/" + request.fbid,
                    Type  = "openUrl",
                    Title = request.name
                };
                dynamic quickReplies  = new JObject();
                dynamic fbQRButtonRed = new JObject();
                fbQRButtonRed.content_type = "text";
                fbQRButtonRed.title        = "YES";
                fbQRButtonRed.payload      = String.Format("_YES_a_{0}_a_{1}_", request.id.ToString(), activity.From.Id.ToString());
                //fbQRButtonRed.payload = String.Format("{\r\n\"Answer\":\"YES\",\r\n\"Id\":\"{0}\",\r\n\"psid\":\"{1}\"\r\n}",request.id.ToString(),activity.From.Id.ToString());
                fbQRButtonRed.image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Button_Icon_Red.svg/300px-Button_Icon_Red.svg.png";
                dynamic fbQRButtonBlue = new JObject();
                fbQRButtonBlue.content_type = "text";
                fbQRButtonBlue.title        = "NO";
                fbQRButtonBlue.payload      = String.Format("_NO_a_{0}_a_{1}_", request.id.ToString(), activity.From.Id.ToString());
                //fbQRButtonBlue.payload = String.Format("{\"Answer\":\"NO\",\"Id\":\"{0}\",\"psid\":\"{1}\"", request.id, activity.From.Id);
                fbQRButtonBlue.image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Button_Icon_Blue.svg/768px-Button_Icon_Blue.svg.png";

                quickReplies.quick_replies = new JArray(fbQRButtonRed, fbQRButtonBlue);
                message.ChannelData        = quickReplies;
                await connector.Conversations.SendToConversationAsync((Activity)message);

                return(1);
            }
            else if (x.topScoringIntent.intent == "Search")
            {
                var a = await GetEntityFromLUIS(activity.Text);

                var y = await DBquery.Clean(a);

                var z = await DBquery.dataquery(y);

                z = z.Where(b => b.psid != activity.From.Id).ToList();
                if (z.Count == 0)
                {
                    await JoinCard.show(activity, connector, "Sorry. There were no matches. You can create your own pool.");
                }
                await JoinCard.Cards(activity, connector, z);

                return(1);
            }
            else if (x.topScoringIntent.intent == "Add")
            {
                cleandata cleaned = await DBquery.Clean(x);

                if ((cleaned.date == null) || (cleaned.dest == "") || (cleaned.origin == "") || (cleaned.time == default(DateTime)))
                {
                    return(await JoinCard.show(activity, connector, "Please provide complete travel information."));
                }
                else
                {
                    // ask for max no. people
                    //Activity reply = activity.CreateReply("Please specify the number of seats");
                    //await connector.Conversations.ReplyToActivityAsync(reply);
                    var a = await GetUserName(activity);

                    var b = await GetFBid(activity);

                    string y = await DBquery.addquery(cleaned, a, activity.From.Id, b);

                    return(await JoinCard.show(activity, connector, y));
                }
            }
            else if (x.topScoringIntent.intent == "Show")
            {
                string y = await GetUserName(activity);

                var z = await DBquery.showdata(y);

                if (z.Count == 0)
                {
                    await JoinCard.show(activity, connector, "You do not have any Pools. You can join or create you own.");
                }
                else
                {
                    await JoinCard.ShowCards(activity, connector, y, z);
                }
                return(1);
            }

            else if (x.topScoringIntent.intent == "Delete")
            {
                string y = await GetUserName(activity);

                using (travelrecordEntities DB = new travelrecordEntities())
                {
                    var match = (from b in DB.Requests where (b.name == y) select b).ToList();
                    foreach (var b in match)
                    {
                        DB.Requests.Remove(b);
                        await DB.SaveChangesAsync();
                    }
                }
                return(1);
            }
            else
            {
                return(1);
            }
        }