コード例 #1
0
        public Message SendLocation <R, P>(P parameter)
            where R : ReplyMarkup
            where P : SendLocationParameter <R>
        {
            var method = new SendLocation <R, P>();

            return(method.Execute(parameter, HttpClient).Result);
        }
コード例 #2
0
 public static ValidationResult <SendLocation> CreateValidation(this SendLocation value) => new ValidationResult <SendLocation>(value)
 .ValidateRequired(x => x.ChatId)
 .ValidateRequired(x => x.Latitude)
 .ValidateRequired(x => x.Longitude)
 .IsFalse(x => x.Heading != null && x.Heading < 1 && x.Heading > 360, ValidationErrors.HeadingOutOfRange)
 .IsFalse(x => x.ProximityAlertRadius != null && x.ProximityAlertRadius < 1 && x.ProximityAlertRadius > 100000,
          ValidationErrors.ProximityAlertRadiusOutOfRange)
 .IsFalse(x => x.HorizontalAccuracy != null && x.ProximityAlertRadius < 0 && x.ProximityAlertRadius > 1500,
          ValidationErrors.HorizontalAccuracyOutOfRange);
コード例 #3
0
        public static void RunBot(string accessToken)
        {
            var bot = new TelegramBot(accessToken);

            var me = bot.MakeRequestAsync(new GetMe()).Result;
            if (me == null)
            {
                Console.WriteLine("GetMe() FAILED. Do you forget to add your AccessToken to config.json?");
                Console.WriteLine("(Press ENTER to quit)");
                Console.ReadLine();
                return;
            }
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("{0} (@{1}) connected!", me.FirstName, me.Username);

            Console.WriteLine();
            Console.WriteLine("Find @{0} in Telegram and send him a message - it will be displayed here", me.Username);
            Console.WriteLine("(Press ENTER to stop listening and quit)");
            Console.WriteLine();

            string uploadedPhotoId = null;
            string uploadedDocumentId = null;
            long offset = 0;
            while (!stopMe)
            {
                var updates = bot.MakeRequestAsync(new GetUpdates() { Offset = offset }).Result;
                if (updates != null)
                {
                    foreach (var update in updates)
                    {
                        offset = update.UpdateId + 1;
                        if (update.Message == null)
                        {
                            continue;
                        }
                        var from = update.Message.From;
                        var text = update.Message.Text;
                        var photos = update.Message.Photo;
                        var contact = update.Message.Contact;
                        var location = update.Message.Location;
                        Console.WriteLine(
                            "Msg from {0} {1} ({2}) at {4}: {3}",
                            from.FirstName,
                            from.LastName,
                            from.Username,
                            text,
                            update.Message.Date);

                        if (photos != null)
                        {
                            var webClient = new HttpClient();
                            foreach (var photo in photos)
                            {
                                Console.WriteLine("  New image arrived: size {1}x{2} px, {3} bytes, id: {0}", photo.FileId, photo.Height, photo.Width, photo.FileSize);
                                var file = bot.MakeRequestAsync(new GetFile(photo.FileId)).Result;
                                var tempFileName = System.IO.Path.GetTempFileName();
                                var bytes = webClient.GetByteArrayAsync(file.FileDownloadUrl).Result;
                                System.IO.File.WriteAllBytes(tempFileName, bytes);
                                Console.WriteLine("    Saved to {0}", tempFileName);
                            }
                        }
                        if (contact != null)
                        {
                            var req = new SendContact(update.Message.Chat.Id, contact.PhoneNumber, contact.FirstName)
                            {
                                LastName = contact.LastName
                            };
                            bot.MakeRequestAsync(req).Wait();
                        }
                        if (location != null)
                        {
                            var req = new SendLocation(update.Message.Chat.Id, location.Latitude, location.Longitude);
                            bot.MakeRequestAsync(req).Wait();
                        }

                        if (string.IsNullOrEmpty(text))
                        {
                            continue;
                        }
                        if (text == "/photo")
                        {
                            if (uploadedPhotoId == null)
                            {
                                var reqAction = new SendChatAction(update.Message.Chat.Id, "upload_photo");
                                bot.MakeRequestAsync(reqAction).Wait();
                                System.Threading.Thread.Sleep(500);
                                using (var photoData = typeof(Program).GetTypeInfo().Assembly.GetManifestResourceStream("TelegramBotDemo-vNext.t_logo.png"))
                                {
                                    var req = new SendPhoto(update.Message.Chat.Id, new FileToSend(photoData, "Telegram_logo.png"))
                                    {
                                        Caption = "Telegram_logo.png"
                                    };
                                    var msg = bot.MakeRequestAsync(req).Result;
                                    uploadedPhotoId = msg.Photo.Last().FileId;
                                }
                            }
                            else
                            {
                                var req = new SendPhoto(update.Message.Chat.Id, new FileToSend(uploadedPhotoId))
                                {
                                    Caption = "Resending photo id=" + uploadedPhotoId
                                };
                                bot.MakeRequestAsync(req).Wait();
                            }
                            continue;
                        }
                        if (text == "/doc")
                        {
                            if (uploadedDocumentId == null)
                            {
                                var reqAction = new SendChatAction(update.Message.Chat.Id, "upload_document");
                                bot.MakeRequestAsync(reqAction).Wait();
                                System.Threading.Thread.Sleep(500);
                                using (var docData = typeof(Program).GetTypeInfo().Assembly.GetManifestResourceStream("TelegramBotDemo-vNext.Telegram_Bot_API.htm"))
                                {
                                    var req = new SendDocument(update.Message.Chat.Id, new FileToSend(docData, "Telegram_Bot_API.htm"));
                                    var msg = bot.MakeRequestAsync(req).Result;
                                    uploadedDocumentId = msg.Document.FileId;
                                }
                            }
                            else
                            {
                                var req = new SendDocument(update.Message.Chat.Id, new FileToSend(uploadedDocumentId));
                                bot.MakeRequestAsync(req).Wait();
                            }
                            continue;
                        }
                        if (text == "/docutf8")
                        {
                            var reqAction = new SendChatAction(update.Message.Chat.Id, "upload_document");
                            bot.MakeRequestAsync(reqAction).Wait();
                            System.Threading.Thread.Sleep(500);
                            using (var docData = typeof(Program).GetTypeInfo().Assembly.GetManifestResourceStream("TelegramBotDemo-vNext.Пример_UTF8_filename.txt"))
                            {
                                var req = new SendDocument(update.Message.Chat.Id, new FileToSend(docData, "Пример_UTF8_filename.txt"));
                                var msg = bot.MakeRequestAsync(req).Result;
                                uploadedDocumentId = msg.Document.FileId;
                            }
                            continue;
                        }
                        if (text == "/help")
                        {
                            var keyb = new ReplyKeyboardMarkup()
                            {
                                Keyboard = new[] {
                                    new[] { new KeyboardButton("/photo"), new KeyboardButton("/doc"), new KeyboardButton("/docutf8") },
                                    new[] { new KeyboardButton("/contact") { RequestContact = true }, new KeyboardButton("/location") { RequestLocation = true } },
                                    new[] { new KeyboardButton("/help") }
                                },
                                OneTimeKeyboard = true,
                                ResizeKeyboard = true
                            };
                            var reqAction = new SendMessage(update.Message.Chat.Id, "Here is all my commands") { ReplyMarkup = keyb };
                            bot.MakeRequestAsync(reqAction).Wait();
                            continue;
                        }
                        if (text == "/longmsg")
                        {
                            var msg = new string('X', 10240);
                            bot.MakeRequestAsync(new SendMessage(update.Message.Chat.Id, msg)).Wait();
                            continue;
                        }
                        if (update.Message.Text.Length % 2 == 0)
                        {
                            bot.MakeRequestAsync(new SendMessage(
                                update.Message.Chat.Id,
                                "You wrote *" + update.Message.Text.Length + " characters*")
                            {
                                ParseMode = SendMessage.ParseModeEnum.Markdown
                            }).Wait();
                        }
                        else
                        {
                            bot.MakeRequestAsync(new ForwardMessage(update.Message.Chat.Id, update.Message.Chat.Id, update.Message.MessageId)).Wait();
                        }
                    }
                }
            }
        }
コード例 #4
0
        public static void RunBot(string accessToken)
        {
            var bot = new TelegramBot(accessToken, new HttpClient());

            var me = bot.MakeRequestAsync(new GetMe()).Result;

            if (me == null)
            {
                Console.WriteLine("GetMe() FAILED. Do you forget to add your AccessToken to config.json?");
                Console.WriteLine("(Press ENTER to quit)");
                Console.ReadLine();
                return;
            }
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("{0} (@{1}) connected!", me.FirstName, me.Username);

            Console.WriteLine();
            Console.WriteLine("Find @{0} in Telegram and send him a message - it will be displayed here", me.Username);
            Console.WriteLine("(Press ENTER to stop listening and quit)");
            Console.WriteLine();

            string uploadedPhotoId    = null;
            string uploadedDocumentId = null;
            long   offset             = 0;

            while (!stopMe)
            {
                var updates = bot.MakeRequestAsync(new GetUpdates()
                {
                    Offset = offset
                }).Result;
                if (updates != null)
                {
                    foreach (var update in updates)
                    {
                        offset = update.UpdateId + 1;
                        if (update.Message == null)
                        {
                            continue;
                        }
                        var from     = update.Message.From;
                        var text     = update.Message.Text;
                        var photos   = update.Message.Photo;
                        var contact  = update.Message.Contact;
                        var location = update.Message.Location;
                        Console.WriteLine(
                            "Msg from {0} {1} ({2}) at {4}: {3}",
                            from.FirstName,
                            from.LastName,
                            from.Username,
                            text,
                            update.Message.Date);

                        if (photos != null)
                        {
                            var webClient = new HttpClient();
                            foreach (var photo in photos)
                            {
                                Console.WriteLine("  New image arrived: size {1}x{2} px, {3} bytes, id: {0}", photo.FileId, photo.Height, photo.Width, photo.FileSize);
                                var file         = bot.MakeRequestAsync(new GetFile(photo.FileId)).Result;
                                var tempFileName = System.IO.Path.GetTempFileName();
                                var bytes        = webClient.GetByteArrayAsync(file.FileDownloadUrl).Result;
                                System.IO.File.WriteAllBytes(tempFileName, bytes);
                                Console.WriteLine("    Saved to {0}", tempFileName);
                            }
                        }
                        if (contact != null)
                        {
                            var req = new SendContact(update.Message.Chat.Id, contact.PhoneNumber, contact.FirstName)
                            {
                                LastName = contact.LastName
                            };
                            bot.MakeRequestAsync(req).Wait();
                        }
                        if (location != null)
                        {
                            var req = new SendLocation(update.Message.Chat.Id, location.Latitude, location.Longitude);
                            bot.MakeRequestAsync(req).Wait();
                        }

                        if (string.IsNullOrEmpty(text))
                        {
                            continue;
                        }
                        if (text == "/photo")
                        {
                            if (uploadedPhotoId == null)
                            {
                                var reqAction = new SendChatAction(update.Message.Chat.Id, "upload_photo");
                                bot.MakeRequestAsync(reqAction).Wait();
                                System.Threading.Thread.Sleep(500);
                                using var photoData = typeof(Program).GetTypeInfo().Assembly.GetManifestResourceStream("TelegramBotDemo-vNext.t_logo.png");
                                var req = new SendPhoto(update.Message.Chat.Id, new FileToSend(photoData, "Telegram_logo.png"))
                                {
                                    Caption = "Telegram_logo.png"
                                };
                                var msg = bot.MakeRequestAsync(req).Result;
                                uploadedPhotoId = msg.Photo.Last().FileId;
                            }
                            else
                            {
                                var req = new SendPhoto(update.Message.Chat.Id, new FileToSend(uploadedPhotoId))
                                {
                                    Caption = "Resending photo id=" + uploadedPhotoId
                                };
                                bot.MakeRequestAsync(req).Wait();
                            }
                            continue;
                        }
                        if (text == "/doc")
                        {
                            if (uploadedDocumentId == null)
                            {
                                var reqAction = new SendChatAction(update.Message.Chat.Id, "upload_document");
                                bot.MakeRequestAsync(reqAction).Wait();
                                System.Threading.Thread.Sleep(500);
                                using var docData = typeof(Program).GetTypeInfo().Assembly.GetManifestResourceStream("TelegramBotDemo-vNext.Telegram_Bot_API.htm");
                                var req = new SendDocument(update.Message.Chat.Id, new FileToSend(docData, "Telegram_Bot_API.htm"));
                                var msg = bot.MakeRequestAsync(req).Result;
                                uploadedDocumentId = msg.Document.FileId;
                            }
                            else
                            {
                                var req = new SendDocument(update.Message.Chat.Id, new FileToSend(uploadedDocumentId));
                                bot.MakeRequestAsync(req).Wait();
                            }
                            continue;
                        }
                        if (text == "/docutf8")
                        {
                            var reqAction = new SendChatAction(update.Message.Chat.Id, "upload_document");
                            bot.MakeRequestAsync(reqAction).Wait();
                            System.Threading.Thread.Sleep(500);
                            using var docData = typeof(Program).GetTypeInfo().Assembly.GetManifestResourceStream("TelegramBotDemo-vNext.Пример_UTF8_filename.txt");
                            var req = new SendDocument(update.Message.Chat.Id, new FileToSend(docData, "Пример_UTF8_filename.txt"));
                            var msg = bot.MakeRequestAsync(req).Result;
                            uploadedDocumentId = msg.Document.FileId;
                            continue;
                        }
                        if (text == "/help")
                        {
                            var keyb = new ReplyKeyboardMarkup()
                            {
                                Keyboard = new[] {
                                    new[] { new KeyboardButton("/photo"), new KeyboardButton("/doc"), new KeyboardButton("/docutf8") },
                                    new[] { new KeyboardButton("/contact")
                                            {
                                                RequestContact = true
                                            }, new KeyboardButton("/location")
                                            {
                                                RequestLocation = true
                                            } },
                                    new[] { new KeyboardButton("/help") }
                                },
                                OneTimeKeyboard = true,
                                ResizeKeyboard  = true
                            };
                            var reqAction = new SendMessage(update.Message.Chat.Id, "Here is all my commands")
                            {
                                ReplyMarkup = keyb
                            };
                            bot.MakeRequestAsync(reqAction).Wait();
                            continue;
                        }
                        if (text == "/longmsg")
                        {
                            var msg = new string('X', 10240);
                            bot.MakeRequestAsync(new SendMessage(update.Message.Chat.Id, msg)).Wait();
                            continue;
                        }
                        if (update.Message.Text.Length % 2 == 0)
                        {
                            bot.MakeRequestAsync(new SendMessage(
                                                     update.Message.Chat.Id,
                                                     "You wrote: \r\n" + update.Message.Text.MarkdownEncode())
                            {
                                ParseMode = SendMessage.ParseModeEnum.Markdown
                            }).Wait();
                        }
                        else
                        {
                            bot.MakeRequestAsync(new ForwardMessage(update.Message.Chat.Id, update.Message.Chat.Id, update.Message.MessageId)).Wait();
                        }
                    }
                }
            }
        }
コード例 #5
0
        void ReleaseDesignerOutlets()
        {
            if (BackButton != null)
            {
                BackButton.Dispose();
                BackButton = null;
            }

            if (BottomConstraint != null)
            {
                BottomConstraint.Dispose();
                BottomConstraint = null;
            }

            if (DistanceText != null)
            {
                DistanceText.Dispose();
                DistanceText = null;
            }

            if (EditSelf != null)
            {
                EditSelf.Dispose();
                EditSelf = null;
            }

            if (EditSelfBack != null)
            {
                EditSelfBack.Dispose();
                EditSelfBack = null;
            }

            if (EditSelfHeader != null)
            {
                EditSelfHeader.Dispose();
                EditSelfHeader = null;
            }

            if (EditSpacer != null)
            {
                EditSpacer.Dispose();
                EditSpacer = null;
            }

            if (HeaderBackground != null)
            {
                HeaderBackground.Dispose();
                HeaderBackground = null;
            }

            if (HideButton != null)
            {
                HideButton.Dispose();
                HideButton = null;
            }

            if (LastActiveDate != null)
            {
                LastActiveDate.Dispose();
                LastActiveDate = null;
            }

            if (LikeButton != null)
            {
                LikeButton.Dispose();
                LikeButton = null;
            }

            if (LocationTime != null)
            {
                LocationTime.Dispose();
                LocationTime = null;
            }

            if (MapBottomSeparator != null)
            {
                MapBottomSeparator.Dispose();
                MapBottomSeparator = null;
            }

            if (MapSatellite != null)
            {
                MapSatellite.Dispose();
                MapSatellite = null;
            }

            if (MapStreet != null)
            {
                MapStreet.Dispose();
                MapStreet = null;
            }

            if (MapTopSeparator != null)
            {
                MapTopSeparator.Dispose();
                MapTopSeparator = null;
            }

            if (MenuBlock != null)
            {
                MenuBlock.Dispose();
                MenuBlock = null;
            }

            if (MenuContainer != null)
            {
                MenuContainer.Dispose();
                MenuContainer = null;
            }

            if (MenuIcon != null)
            {
                MenuIcon.Dispose();
                MenuIcon = null;
            }

            if (MenuLayer != null)
            {
                MenuLayer.Dispose();
                MenuLayer = null;
            }

            if (MenuReport != null)
            {
                MenuReport.Dispose();
                MenuReport = null;
            }

            if (Name != null)
            {
                Name.Dispose();
                Name = null;
            }

            if (NavigationSpacer != null)
            {
                NavigationSpacer.Dispose();
                NavigationSpacer = null;
            }

            if (NextButton != null)
            {
                NextButton.Dispose();
                NextButton = null;
            }

            if (PercentProgress != null)
            {
                PercentProgress.Dispose();
                PercentProgress = null;
            }

            if (PreviousButton != null)
            {
                PreviousButton.Dispose();
                PreviousButton = null;
            }

            if (ProfileImageContainer != null)
            {
                ProfileImageContainer.Dispose();
                ProfileImageContainer = null;
            }

            if (ProfileImageScroll != null)
            {
                ProfileImageScroll.Dispose();
                ProfileImageScroll = null;
            }

            if (ProfileViewDescription != null)
            {
                ProfileViewDescription.Dispose();
                ProfileViewDescription = null;
            }

            if (ProfileViewMap != null)
            {
                ProfileViewMap.Dispose();
                ProfileViewMap = null;
            }

            if (ProfileViewScroll != null)
            {
                ProfileViewScroll.Dispose();
                ProfileViewScroll = null;
            }

            if (RegisterDate != null)
            {
                RegisterDate.Dispose();
                RegisterDate = null;
            }

            if (ResponseRate != null)
            {
                ResponseRate.Dispose();
                ResponseRate = null;
            }

            if (RippleImageNext != null)
            {
                RippleImageNext.Dispose();
                RippleImageNext = null;
            }

            if (RippleImagePrev != null)
            {
                RippleImagePrev.Dispose();
                RippleImagePrev = null;
            }

            if (RippleProfileView != null)
            {
                RippleProfileView.Dispose();
                RippleProfileView = null;
            }

            if (RoundBottom != null)
            {
                RoundBottom.Dispose();
                RoundBottom = null;
            }

            if (ScrollBottomConstraint != null)
            {
                ScrollBottomConstraint.Dispose();
                ScrollBottomConstraint = null;
            }

            if (SendLocation != null)
            {
                SendLocation.Dispose();
                SendLocation = null;
            }

            if (Snackbar != null)
            {
                Snackbar.Dispose();
                Snackbar = null;
            }

            if (SnackBottomConstraint != null)
            {
                SnackBottomConstraint.Dispose();
                SnackBottomConstraint = null;
            }

            if (SnackTopConstraint != null)
            {
                SnackTopConstraint.Dispose();
                SnackTopConstraint = null;
            }

            if (Username != null)
            {
                Username.Dispose();
                Username = null;
            }

            if (ViewportConstraint != null)
            {
                ViewportConstraint.Dispose();
                ViewportConstraint = null;
            }
        }