Как отправить сообщение telegram bot, используя asp-net WEBAPI c#?

Рейтинг: 0Ответов: 1Опубликовано: 03.08.2023

Создал последовательно проект по https://habr.com/ru/sandbox/188818/. Однако не смог разобрать, каким образом такой проект отправляет сообщение "Привет", используя [HttpPost], метод Post:

[HttpPost]
public async void Post(Update update)
{
  long chatId = update.Message.Chat.Id; //получаем айди чата, куда нам сказать привет
  await bot.SendTextMessageAsync(chatId, "Привет!");
}   

Bot.cs

using Telegram.Bot;

namespace TelegramService
{
    public class Bot  //паттерн Singleton
    {
        private static TelegramBotClient client { get; set; }

        public static TelegramBotClient GetTelegramBot()
        {
            if (client != null)
            {
                return client;
            }
            //client = new TelegramBotClient("ваш токен");
            client = new TelegramBotClient("6648603010:AAEeTZT63Cy3p6ys-iQKs6EUV4gVkvauc8Y");
            return client;
        }
    }
}

BotController.cs

using Microsoft.AspNetCore.Mvc;
using Telegram.Bot;
using Telegram.Bot.Types;

namespace TelegramService.Controllers
{
    [ApiController]
    [Route("/")]
    public class BotController : Controller
    {
        private TelegramBotClient bot = Bot.GetTelegramBot();
        //private UpdateDistributor<CommandExecutor> updateDistributor = new UpdateDistributor<CommandExecutor>();

        private string chatText;
        private long chatId;

        [HttpPost]
        public async void Post(Update update) //Сюда будут приходить апдейты
        {
            chatId = update.Message.Chat.Id;
            if (update.Message == null) //и такое тоже бывает, делаем проверку
                return;

            await bot.SendTextMessageAsync(chatId, "Привет!");

            //await updateDistributor.GetUpdate(update);

        }
        [HttpGet]
        public string Get()
        {
            //Здесь мы пишем, что будет видно если зайти на адрес,
            //указаную в ngrok и launchSettings
            var me = bot.GetMeAsync().Result;
            
            var text = $"Telegram bot {me.Username} was started. Text: {chatText}, chatId: {chatId}";
            return text;
        }
    }
}

Используя Postman, отправляю post запрос. Выводит:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
    "title": "Unsupported Media Type",
    "status": 415,
    "traceId": "00-65dd9614fe76c3f2fa50a97ef4435cf9-67d899bb89be2113-00"
}

Используя Swagger, работа post выводит:Вывод работы Post

Response body:

{
  "errors": {
    "update": [
      "The update field is required."
    ],
    "update_id": [
      "Required property 'update_id' not found in JSON. Path '', line 4362, position 1."
    ],
    "poll.is_closed": [
      "Required property 'is_closed' not found in JSON. Path 'poll', line 3970, position 3."
    ],
    "poll.is_anonymous": [
      "Required property 'is_anonymous' not found in JSON. Path 'poll', line 3970, position 3."
    ],
    "poll.total_voter_count": [
      "Required property 'total_voter_count' not found in JSON. Path 'poll', line 3970, position 3."
    ],
    "poll.allows_multiple_answers": [
      "Required property 'allows_multiple_answers' not found in JSON. Path 'poll', line 3970, position 3."
    ],
    "message.date": [
      "Unexpected token parsing date. Expected Integer or String, got Date. Path 'message.date', line 81, position 38."
    ],
    "message.message_id": [
      "Required property 'message_id' not found in JSON. Path 'message', line 761, position 3."
    ],
    "message.from.is_bot": [
      "Required property 'is_bot' not found in JSON. Path 'message.from', line 18, position 5."
    ],
    "message.from.first_name": [
      "Required property 'first_name' not found in JSON. Path 'message.from', line 18, position 5."
    ],
    "message.poll.is_closed": [
      "Required property 'is_closed' not found in JSON. Path 'message.poll', line 526, position 5."
    ],
    "message.poll.is_anonymous": [
      "Required property 'is_anonymous' not found in JSON. Path 'message.poll', line 526, position 5."
    ],
    "message.poll.total_voter_count": [
      "Required property 'total_voter_count' not found in JSON. Path 'message.poll', line 526, position 5."
    ],
    "message.poll.allows_multiple_answers": [
      "Required property 'allows_multiple_answers' not found in JSON. Path 'message.poll', line 526, position 5."
    ],
    "message.audio.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.audio', line 300, position 5."
    ],
    "message.audio.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.audio', line 300, position 5."
    ],
    "message.video.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.video', line 373, position 5."
    ],
    "message.video.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.video', line 373, position 5."
    ],
    "message.voice.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.voice', line 394, position 5."
    ],
    "message.voice.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.voice', line 394, position 5."
    ],
    "message.contact.first_name": [
      "Required property 'first_name' not found in JSON. Path 'message.contact', line 426, position 5."
    ],
    "message.contact.phone_number": [
      "Required property 'phone_number' not found in JSON. Path 'message.contact', line 426, position 5."
    ],
    "message.invoice.total_amount": [
      "Required property 'total_amount' not found in JSON. Path 'message.invoice', line 605, position 5."
    ],
    "message.invoice.start_parameter": [
      "Required property 'start_parameter' not found in JSON. Path 'message.invoice', line 605, position 5."
    ],
    "message.sticker.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.sticker', line 356, position 5."
    ],
    "message.sticker.is_video": [
      "Required property 'is_video' not found in JSON. Path 'message.sticker', line 356, position 5."
    ],
    "message.sticker.is_animated": [
      "Required property 'is_animated' not found in JSON. Path 'message.sticker', line 356, position 5."
    ],
    "message.sticker.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.sticker', line 356, position 5."
    ],
    "message.document.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.document', line 314, position 5."
    ],
    "message.document.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.document', line 314, position 5."
    ],
    "message.animation.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.animation', line 283, position 5."
    ],
    "message.animation.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.animation', line 283, position 5."
    ],
    "poll.options[0].voter_count": [
      "Required property 'voter_count' not found in JSON. Path 'poll.options[0]', line 3936, position 7."
    ],
    "message.chat.photo.big_file_id": [
      "Required property 'big_file_id' not found in JSON. Path 'message.chat.photo', line 95, position 7."
    ],
    "message.chat.photo.small_file_id": [
      "Required property 'small_file_id' not found in JSON. Path 'message.chat.photo', line 95, position 7."
    ],
    "message.chat.photo.big_file_unique_id": [
      "Required property 'big_file_unique_id' not found in JSON. Path 'message.chat.photo', line 95, position 7."
    ],
    "message.chat.photo.small_file_unique_id": [
      "Required property 'small_file_unique_id' not found in JSON. Path 'message.chat.photo', line 95, position 7."
    ],
    "message.chat.permissions.can_send_audios": [
      "Required property 'can_send_audios' not found in JSON. Path 'message.chat.permissions', line 123, position 7."
    ],
    "message.chat.permissions.can_send_photos": [
      "Required property 'can_send_photos' not found in JSON. Path 'message.chat.permissions', line 123, position 7."
    ],
    "message.chat.permissions.can_send_videos": [
      "Required property 'can_send_videos' not found in JSON. Path 'message.chat.permissions', line 123, position 7."
    ],
    "message.chat.permissions.can_send_documents": [
      "Required property 'can_send_documents' not found in JSON. Path 'message.chat.permissions', line 123, position 7."
    ],
    "message.chat.permissions.can_send_video_notes": [
      "Required property 'can_send_video_notes' not found in JSON. Path 'message.chat.permissions', line 123, position 7."
    ],
    "message.chat.permissions.can_send_voice_notes": [
      "Required property 'can_send_voice_notes' not found in JSON. Path 'message.chat.permissions', line 123, position 7."
    ],
    "message.game.animation.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.game.animation', line 483, position 7."
    ],
    "message.game.animation.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.game.animation', line 483, position 7."
    ],
    "message.audio.thumbnail.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.audio.thumbnail', line 299, position 7."
    ],
    "message.audio.thumbnail.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.audio.thumbnail', line 299, position 7."
    ],
    "message.photo[0].file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.photo[0]', line 322, position 7."
    ],
    "message.photo[0].file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.photo[0]', line 322, position 7."
    ],
    "message.video.thumbnail.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.video.thumbnail', line 370, position 7."
    ],
    "message.video.thumbnail.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.video.thumbnail', line 370, position 7."
    ],
    "message.sticker.thumbnail.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.sticker.thumbnail', line 339, position 7."
    ],
    "message.sticker.thumbnail.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.sticker.thumbnail', line 339, position 7."
    ],
    "message.document.thumbnail.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.document.thumbnail', line 311, position 7."
    ],
    "message.document.thumbnail.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.document.thumbnail', line 311, position 7."
    ],
    "message.animation.thumbnail.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.animation.thumbnail', line 280, position 7."
    ],
    "message.animation.thumbnail.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.animation.thumbnail', line 280, position 7."
    ],
    "message.game.photo[0].file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.game.photo[0]', line 441, position 9."
    ],
    "message.game.photo[0].file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.game.photo[0]', line 441, position 9."
    ],
    "message.game.animation.thumbnail.file_id": [
      "Required property 'file_id' not found in JSON. Path 'message.game.animation.thumbnail', line 480, position 9."
    ],
    "message.game.animation.thumbnail.file_unique_id": [
      "Required property 'file_unique_id' not found in JSON. Path 'message.game.animation.thumbnail', line 480, position 9."
    ],
    "message.poll.options[0].voter_count": [
      "Required property 'voter_count' not found in JSON. Path 'message.poll.options[0]', line 492, position 9."
    ],
    "message.entities[0].user.is_bot": [
      "Required property 'is_bot' not found in JSON. Path 'message.entities[0].user', line 262, position 9."
    ],
    "message.entities[0].user.first_name": [
      "Required property 'first_name' not found in JSON. Path 'message.entities[0].user', line 262, position 9."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-a863b219371f2b78681dc06df7eb11af-aaf6ecaf2357c1ce-00"
}

Проблемы со связю: ngrok

Ответы

▲ 0

Ошибка была в том, что я использовал токены разных ботов в настройках и при установке вебхука