コード例 #1
0
        public static void Run_Bot()
        {
            /*first
             * //Create bot
             * var bot = new TelegramBot(Token);
             * var me = bot.MakeRequestAsync(new GetMe()).Result;
             *
             *
             * //creating for each user a specific id to have it's own state
             * var userId = new Dictionary<long, int>();
             * int cnt = 0;
             *
             * long offset = 0;
             * long iid = 0;
             *
             * Tourist_BotEntities _context = new Tourist_BotEntities();
             *
             * while (true)
             * {
             *     var updates = bot.MakeRequestAsync(new GetUpdates() { Offset = offset }).Result;
             *     try
             *     {
             *         foreach (var update in updates)
             *         {
             *             Console.WriteLine(update.Message.Text);
             *             string text = update.Message.Text;
             *             long chatId = update.Message.Chat.Id;
             *             if (text == "/start")
             *             {
             *                 string message = "welcom :)";
             *                 var reg = new SendMessage(chatId, message);
             *                 bot.MakeRequestAsync(reg);
             *             }
             *             else
             *             {
             *                 City city = new City();
             *                 city.Id = iid;
             *                 city.Message = text;
             *                 _context.Cities.Add(city);
             *                 try { _context.SaveChanges(); }
             *                 catch (Exception e) { Console.WriteLine(e.Message); }
             *                 string message = "undefined request !";
             *                 var reg = new SendMessage(chatId, message);
             *                 bot.MakeRequestAsync(reg);
             *             }
             *             offset = update.UpdateId + 1;
             *             iid++;
             *         }
             *     }
             *     catch (Exception e) { Console.WriteLine(e.Message); }
             * }*/

            /*second*/
            //making the bot
            var bot = new TelegramBot(Token);
            var me  = bot.MakeRequestAsync(new GetMe()).Result;


            //creating for each user a specific id to have it's own state
            var userId = new Dictionary <long, int>();
            int cnt    = 0;

            //getting the list of person in ram
            List <Person> persons = new List <Person>();
            long          offset  = 0;
            long          iid     = 0;
            //long examIDs = 0;
            //long QuestionIDs = 0;

            Tourist_BotEntities _context = new Tourist_BotEntities();

            while (true)
            {
                var updates = bot.MakeRequestAsync(new GetUpdates()
                {
                    Offset = offset
                }).Result;
                try
                {
                    foreach (var update in updates)
                    {
                        long chatId = update.Message.Chat.Id;

                        if (!userId.ContainsKey(chatId))
                        {
                            userId.Add(chatId, cnt);
                            Person p = new Person();
                            p.ChatID = chatId;
                            p.State  = "Start";

                            //p.pic = update.Message.Photo.ToString();
                            persons.Add(p);
                            cnt++;
                        }
                        var check = _context.Users.Where(x => x.Id == chatId);
                        //Console.WriteLine(check.ToList()[0].Id);
                        if (check.Count() != 0)
                        {
                            var temp = _context.Users.FirstOrDefault(x => x.Id == chatId);
                            _context.Users.Remove(temp);
                        }
                        // _context.Users.SqlQuery("DELETE FROM[Tourist_Bot].[dbo].[User] WHERE[Tourist_Bot].[dbo].[User].Id = " + chatId);

                        User u = new User();

                        u.Id = chatId;
                        if (!persons[userId[chatId]].np_IsP)
                        {
                            u.Message = update.Message.Text;
                        }
                        else
                        {
                            u.Message = "IsPhoto";
                        }
                        u.State     = persons.Where(x => x.ChatID == chatId).ToList()[0].State;
                        u.Username  = update.Message.Chat.Username;
                        u.Firstname = update.Message.Chat.FirstName;
                        u.Lastname  = update.Message.Chat.LastName;
                        _context.Users.Add(u);



                        //    Console.WriteLine("avali");


                        //else

                        //  Console.Write("DELETE FROM[Tourist_Bot].[dbo].[User] WHERE[Tourist_Bot].[dbo].[User].Id = " + chatId);
                        //  _context.Users.SqlQuery("DELETE FROM[Tourist_Bot].[dbo].[User] WHERE[Tourist_Bot].[dbo].[User].Id = 60163330");
                        //_context.Users.S

                        //_context.Users.

                        try { _context.SaveChanges(); }
                        catch (Exception e) { Console.WriteLine(e.Message + "DB in ConnectToDB"); }


                        persons[userId[chatId]].Text = update.Message.Text;
                        //persons[userId[chatId]].Text = "Null";
                        //MemoryStream ms = new MemoryStream();
                        if (!persons[userId[chatId]].np_IsP || persons[userId[chatId]].Text == "انصراف")
                        {
                            //   Console.WriteLine("omad");
                            persons[userId[chatId]].Text = update.Message.Text;
                        }
                        else
                        {
                            persons[userId[chatId]].Pic = update.Message.Photo;
                            //persons[userId[chatId]].Pic = update.Message.GetType;
                            // Console.WriteLine(update.Message.GetType());

                            //persons[userId[chatId]].Pic.FileId = "f**k";
                            //   Console.Write("ax" + persons[userId[chatId]].Pic[0].FileId);
                            // string FUCKU = persons[userId[chatId]].Pic.FileId;
                        }
                        // Console.WriteLine(persons[userId[chatId]].State);
                        StateDesignPattern userState = new StateDesignPattern();
                        //   Console.WriteLine("mire");
                        userState.CheckState(persons[userId[chatId]], bot, _context);
                        offset = update.UpdateId + 1;
                        //  Console.WriteLine("mire2");
                        iid++;
                    }
                }
                catch (Exception e) { Console.WriteLine(e.Message + e.Source + e.GetType() + e.Data + "Base error"); }
            }
        }
コード例 #2
0
        //DBH DB = new DBH();

        // public DBHelper Db { get => db; set => db = value; }

        public void CheckState(Person person, TelegramBot bot, Tourist_BotEntities _context)
        {
            //User user = _context.Users.FirstOrDefault(x => x.Id == person.ChatID);  //comment**: if its necessary to keep track of repeated users
            //Console.WriteLine("");
            //if (user == null)
            //{
            //    //Console.WriteLine("");
            //    User newUser = new User();
            //    newUser.Id = person.ChatID;
            //    newUser.Message = person.Text;
            //    newUser.State = person.State;
            //    _context.Users.Add(newUser);
            //    try
            //    {
            //        _context.SaveChanges();
            //    }
            //    catch (Exception e) { Console.WriteLine(e.Message); }
            //}
            if (person.State == "Start" || person.Text == "Start" || person.Text == "start")
            ////////////inke dar state e city bkhahad ostan ra avaz knad dar enteha ezafe shavad.
            {
                string message = "به راهنمای ایران گردی خوش آمدید :)";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.StartState()
                };

                bot.MakeRequestAsync(reg);
                //   Console.WriteLine("avali");
                person.State = "Options";//taeen ink mkhahad ostan ezafe knd ya list bebinad
            }
            else if (person.State == "Options" && person.Text == "انتخاب استان")
            {
                string message = "استان مورد نظر خود را انتخاب کنید:";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.ProvinceState()
                };

                bot.MakeRequestAsync(reg);
                person.State = "City";
                //    Console.WriteLine("dovomi");
            }
            //else if (person.State == "City" && (person.Text == "تهران" || person.Text == "فارس"))// agar yek ostane alaki type shavad, nabaid peiqame entekhabe shahr namayesh dade shavad , ama in baiad dynamic shavad
            else if (person.State == "City" && person.Text != "انصراف")
            {
                string message = "شهر مورد نظر خود را انتخاب کنید:";
                long   proID   = _context.Provinces.Where(x => x.Name == person.Text).ToList()[0].Id;
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.CityState(proID)
                };

                bot.MakeRequestAsync(reg);
                person.State = "Places";
                //  Console.WriteLine("dovomi");
            }

            /* else if (person.State == "Places" && person.Text == "")
             *   //////////chetori hame shahraye hame ostanaro check knm?? age ye halate koliam baram baz moshkele chert type kardan hast
             * {
             *   try
             *   {
             *       string message = "استان مورد نظر خود را انتخاب کنید:";
             *       var reg = new SendMessage(person.ChatID, message) { ReplyMarkup = keyboard.ProvinceState() };
             *
             *       bot.MakeRequestAsync(reg);
             *       person.State = "";
             *       Console.WriteLine("");
             *   }
             *   catch (Exception e)
             *   {
             *
             *       Console.WriteLine(e.Message);
             *   }
             *
             * }*///hich idei nadaram ina chian ... comment mikonam ziresh ok mikonam

            else if (person.State == "Places" && person.Text != "انصراف")
            {
                string message = "مکان های دیدنی به شرح زیر است. برای اطلاعات بیشتر مکان مورد نظر را انتخاب کنید";
                long   CitID   = _context.Cities.Where(x => x.Name == person.Text).ToList()[0].Id;
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.PlaceState(CitID)
                };

                bot.MakeRequestAsync(reg);
                person.State = "Desc";
            }
            else if (person.State == "Desc" && person.Text != "انصراف")
            {
                string     message = _context.Places.Where(x => x.Name == person.Text).ToList()[0].Description;
                FileToSend F       = new FileToSend(_context.Places.Where(x => x.Name == person.Text).ToList()[0].Photo);
                bot.MakeRequestAsync(new SendPhoto(person.ChatID, F));
                var reg = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.StartState()
                };

                bot.MakeRequestAsync(reg);
                person.State = "Options";
            }
            //else if (person.State == "Places" && person.Text == "خروج")////////////in halat baarye tamame state ha baiad gozashte shavad
            else if (person.Text == "خروج")
            {
                string message = "خدانگهدار. می توانید مجدد آغاز کنید :)";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.OutState()
                };
                bot.MakeRequestAsync(reg);
                person.State = "Out";
                //  Console.WriteLine("sevomi");
            }
            else if (person.State == "Options" && person.Text == "اضافه کردن مکان جدید")
            {
                string message = "نام استان را وارد کنید";
                //  string message = person.pic;
                var reg = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.AddPlace()
                };

                bot.MakeRequestAsync(reg);
                person.State = "addPlace1";
            }
            else if (person.State == "addPlace1" && person.Text != "انصراف")

            {
                person.np_pro = person.Text;
                string message = "نام شهر را وارد کنید";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.AddPlace()
                };

                bot.MakeRequestAsync(reg);
                person.State = "addPlace2";
            }
            else if (person.State == "addPlace2" && person.Text != "انصراف")

            {
                person.np_city = person.Text;
                string message = "نام مکان را وارد کنید";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.AddPlace()
                };

                bot.MakeRequestAsync(reg);
                person.State = "addPlace3";
            }
            else if (person.State == "addPlace3" && person.Text != "انصراف")
            {
                person.np_name = person.Text;
                string message = "توضیحاتی در مورد این مکان وارد کنید";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.AddPlace()
                };
                bot.MakeRequestAsync(reg);
                person.State = "addPlace4";
            }

            else if (person.State == "addPlace4" && person.Text != "انصراف")
            {
                //if ok
                person.np_desc = person.Text;
                Console.WriteLine(person.np_pro + person.np_city + person.np_name + person.np_desc);

                /*  Place pl = new Place();
                 * City c = new City();
                 * Province pr = new Province();
                 * pr.Name = NewPlace.np_pro;
                 * c.Name = NewPlace.np_city;
                 * pl.Description = NewPlace.np_desc;
                 * pl.Name = NewPlace.np_name;
                 * _context.Provinces.Add(pr);
                 * try { _context.SaveChanges(); }
                 * catch (Exception e) { Console.WriteLine(e.Message); }
                 * //while(_context.Provinces.Where(x => x.Name == NewPlace.np_pro).ToList()[0].Id == 0) { }
                 * c.Province_Id = _context.Provinces.Where(x => x.Name == NewPlace.np_pro ).ToList()[0].Id;
                 * _context.Cities.Add(c);
                 * try { _context.SaveChanges(); }
                 * catch (Exception e) { Console.WriteLine(e.Message); }
                 * //while (_context.Cities.Where(x => x.Name == NewPlace.np_city).ToList()[0].Id == 0) { }
                 * pl.City_Id = _context.Cities.Where(x => x.Name == NewPlace.np_city).ToList()[0].Id;
                 * _context.Places.Add(pl);
                 */



                try { _context.SaveChanges(); }
                catch (Exception e) { Console.WriteLine(e.Message + "DB error"); }


                string message = "لطفا عکسی از مکان را ارسال کنید";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.AddPlace()
                };

                bot.MakeRequestAsync(reg);
                person.State  = "Pic";
                person.np_IsP = true;
            }
            else if (person.State == "Pic" && person.Text != "انصراف")
            {
                // NewPlace.np_pic = person.Pic;
                Console.WriteLine(person.np_pro + person.np_city + person.np_name + person.np_desc + person.Pic[0].FileId);
                //  _context.AddPlace("1", "2", "3", "4", "5");
                DBH.AddPlace(person.np_pro, person.np_city, person.np_name, person.np_desc, person.Pic[0].FileId);
                string message = "با تشکر از شما مکان جدید ثبت شد";
                var    reg     = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.StartState()
                };
                // GetChat(person.ChatID).

                bot.MakeRequestAsync(reg);
                person.State  = "Options";
                person.np_IsP = false;
            }
            else if (person.Text == "انصراف")
            {
                string message = "";
                if (person.State == "addPlace1" || person.State == "addPlace2" || person.State == "addPlace3" || person.State == "addPlace4" || person.State == "Pic")
                {
                    message = "اضافه کردن مکان جدید لغو شد";
                }
                else
                {
                    message = "جست و جو مکان لغو شد";
                }
                var reg = new SendMessage(person.ChatID, message)
                {
                    ReplyMarkup = keyboard.StartState()
                };

                bot.MakeRequestAsync(reg);
                person.State = "Options";
            }
            // user.State = person.State;          /////comment**
            //_context.SaveChanges();
        }