Exemplo n.º 1
0
 static async Task RequestContactAndLocation(Message message)
 {
     var requestReplyKeyboard = new ReplyKeyboardMarkup(new[]
     {
         KeyboardButton.WithRequestLocation("Location"),
         KeyboardButton.WithRequestContact("Contact"),
     });
     await Bot.SendTextMessageAsync(
         chatId : message.Chat.Id,
         text : "Who or Where are you?",
         replyMarkup : requestReplyKeyboard
         );
 }
Exemplo n.º 2
0
        private static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            var message = messageEventArgs.Message;

            if (message == null || message.Type != MessageType.Text)
            {
                return;
            }

            switch (message.Text.Split(' ').First())
            {
            // send inline keyboard
            case "/inline":
                await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);

                await Task.Delay(500);     // simulate longer running task

                var inlineKeyboard = new InlineKeyboardMarkup(new[]
                {
                    new []     // first row
                    {
                        InlineKeyboardButton.WithCallbackData("First"),
                        InlineKeyboardButton.WithCallbackData("Second"),
                    },
                    new []     // second row
                    {
                        InlineKeyboardButton.WithCallbackData("First"),
                        InlineKeyboardButton.WithCallbackData("Second"),
                    }
                });

                await Bot.SendTextMessageAsync(
                    message.Chat.Id,
                    "Choose",
                    replyMarkup : inlineKeyboard);

                break;

            // send custom keyboard
            case "/keyboard":
                ReplyKeyboardMarkup ReplyKeyboard = new[]
                {
                    new[] { "First", "Second" },
                    new[] { "First", "Second" },
                };

                await Bot.SendTextMessageAsync(
                    message.Chat.Id,
                    "Choose",
                    replyMarkup : ReplyKeyboard);

                break;

            // request location or contact
            case "/request":
                var RequestReplyKeyboard = new ReplyKeyboardMarkup(new[]
                {
                    KeyboardButton.WithRequestLocation("Location"),
                    KeyboardButton.WithRequestContact("Contact"),
                });

                await Bot.SendTextMessageAsync(
                    message.Chat.Id,
                    "Who or Where are you?",
                    replyMarkup : RequestReplyKeyboard);

                break;

            default:
                const string usage = @"
Usage:
/inline   - send inline keyboard
/keyboard - send custom keyboard
/request  - request location or contact
/subscribe - add to db datas";

                await Bot.SendTextMessageAsync(
                    message.Chat.Id,
                    usage,
                    replyMarkup : new ReplyKeyboardRemove());

                break;
            }
        }
Exemplo n.º 3
0
        static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
        {
            if (update.Type == UpdateType.Message && update.Message != null && update.Message.Type == MessageType.Text)
            {
                long   chatId      = update.Message.Chat.Id;
                string messageText = update.Message.Text;
                if (messageText == "/start")
                {
                    string menu = "/start - запуск\n/inline - меню\n/keyboard - сообщения\n/clearkeyboard - убрать кнопки";
                    await botClient.SendTextMessageAsync(chatId, menu, cancellationToken : cancellationToken);
                }
                else if (messageText == "/inline")
                {
                    InlineKeyboardMarkup inlineKeyboard = new InlineKeyboardMarkup(new[]
                    {
                        new[]
                        {
                            InlineKeyboardButton.WithUrl("VK", "https://vk.com/mishanya167"),
                            InlineKeyboardButton.WithUrl("LinkedIn", "https://www.linkedin.com/in/%D0%BC%D0%B8%D1%85%D0%B0%D0%B8%D0%BB-%D0%BA%D0%BE%D0%B2%D0%B0%D0%BB%D1%91%D0%B2-79637b164/")
                        },
                        new[]
                        {
                            InlineKeyboardButton.WithCallbackData("Дата и время", "DateTime"),
                            InlineKeyboardButton.WithCallbackData("Картинка", "Photo")
                        }
                    });
                    await botClient.SendTextMessageAsync(chatId, "Выберите пункт меню: ", replyMarkup : inlineKeyboard, cancellationToken : cancellationToken);
                }
                else if (messageText == "/keyboard")
                {
                    ReplyKeyboardMarkup replayKeyBoard = new ReplyKeyboardMarkup(new[]
                    {
                        new[]
                        {
                            new KeyboardButton("Привет"),
                            new KeyboardButton("Как дела?")
                        },
                        new[]
                        {
                            KeyboardButton.WithRequestContact("Контакт"),
                            KeyboardButton.WithRequestLocation("Геолокация")
                        }
                    });
                    await botClient.SendTextMessageAsync(chatId, "Выберите сообщение.", replyMarkup : replayKeyBoard, cancellationToken : cancellationToken);
                }
                else if (messageText == "/clearkeyboard")
                {
                    await botClient.SendTextMessageAsync(chatId, "Кнопки убраны.", replyMarkup : new ReplyKeyboardRemove(), cancellationToken : cancellationToken);
                }
                else
                {
                    SessionsClient sessionsClient = await SessionsClient.CreateAsync();

                    SessionName sessionName = new SessionName("small-talk-srqi", Guid.NewGuid().ToString());
                    QueryInput  queryInput  = new QueryInput
                    {
                        Text = new TextInput
                        {
                            Text         = messageText,
                            LanguageCode = "ru-Ru"
                        }
                    };
                    DetectIntentResponse response = await sessionsClient.DetectIntentAsync(sessionName, queryInput);

                    if (response.QueryResult.FulfillmentMessages.Count > 0)
                    {
                        await botClient.SendTextMessageAsync(chatId, response.QueryResult.FulfillmentText, cancellationToken : cancellationToken);
                    }
                    else
                    {
                        await botClient.SendTextMessageAsync(chatId, "Прости, я тебя не понимаю.", cancellationToken : cancellationToken);
                    }
                }
            }
            if (update.Type == UpdateType.CallbackQuery && update.CallbackQuery.Message != null)
            {
                string data = update.CallbackQuery.Data;
                if (data == "DateTime")
                {
                    await botClient.AnswerCallbackQueryAsync(update.CallbackQuery.Id, DateTime.Now.ToString(), cancellationToken : cancellationToken);
                }
                if (data == "Photo")
                {
                    await botClient.SendPhotoAsync(update.CallbackQuery.Message.Chat.Id, "https://big-rostov.ru/wp-content/uploads/2021/04/i-7-1.jpg", cancellationToken : cancellationToken);
                }
            }
        }
        private async Task BotOnMessageReceived(Message message)
        {
            Console.WriteLine($"Receive message type: {message.Type}");
            if (message.Type != MessageType.Text)
            {
                return;
            }

            var action = (message.Text.Split(' ').First()) switch
            {
                "/inline" => SendInlineKeyboard(message),
                "/keyboard" => SendReplyKeyboard(message),
                "/photo" => SendFile(message),
                "/request" => RequestContactAndLocation(message),
                "/help" => Usage(message),
                _ => Nope(message)
            };
            await action;

            // Send inline keyboard
            // You can process responses in BotOnCallbackQueryReceived handler
            async Task SendInlineKeyboard(Message message)
            {
                await _bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);

                // Simulate longer running task
                await Task.Delay(500);

                var inlineKeyboard = new InlineKeyboardMarkup(new[]
                {
                    // first row
                    new []
                    {
                        InlineKeyboardButton.WithCallbackData("1.1", "11"),
                        InlineKeyboardButton.WithCallbackData("1.2", "12"),
                    },
                    // second row
                    new []
                    {
                        InlineKeyboardButton.WithCallbackData("2.1", "21"),
                        InlineKeyboardButton.WithCallbackData("2.2", "22"),
                    }
                });
                await _bot.SendTextMessageAsync(
                    chatId : message.Chat.Id,
                    text : "Choose",
                    replyMarkup : inlineKeyboard
                    );
            }

            async Task SendReplyKeyboard(Message message)
            {
                var replyKeyboardMarkup = new ReplyKeyboardMarkup(
                    new KeyboardButton[][]
                {
                    new KeyboardButton[] { "1.1", "1.2" },
                    new KeyboardButton[] { "2.1", "2.2" },
                },
                    resizeKeyboard: true
                    );

                await _bot.SendTextMessageAsync(
                    chatId : message.Chat.Id,
                    text : "Choose",
                    replyMarkup : replyKeyboardMarkup

                    );
            }

            async Task SendFile(Message message)
            {
                await _bot.SendChatActionAsync(message.Chat.Id, ChatAction.UploadPhoto);

                const string filePath = @"Files/tux.png";

                using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                var fileName = filePath.Split(Path.DirectorySeparatorChar).Last();
                await _bot.SendPhotoAsync(
                    chatId : message.Chat.Id,
                    photo : new InputOnlineFile(fileStream, fileName),
                    caption : "Nice Picture"
                    );
            }

            async Task RequestContactAndLocation(Message message)
            {
                var RequestReplyKeyboard = new ReplyKeyboardMarkup(new[]
                {
                    KeyboardButton.WithRequestLocation("Location"),
                    KeyboardButton.WithRequestContact("Contact"),
                });
                await _bot.SendTextMessageAsync(
                    chatId : message.Chat.Id,
                    text : "Who or Where are you?",
                    replyMarkup : RequestReplyKeyboard
                    );
            }

            async Task Usage(Message message)
            {
                const string usage = "Usage:\n" +
                                     "/inline   - send inline keyboard\n" +
                                     "/keyboard - send custom keyboard\n" +
                                     "/photo    - send a photo\n" +
                                     "/request  - request location or contact";
                await _bot.SendTextMessageAsync(
                    chatId : message.Chat.Id,
                    text : usage,
                    replyMarkup : new ReplyKeyboardRemove()
                    );
            }

            Task Nope(Message message)
            {
                return(Task.CompletedTask);
            }
        }