0

Как отправить фото с текстом, пользователям бота?

1 ответ 1

0

Первый способ прописать команду, затем ссылку на фото, а после описание

using (NpgsqlConnection? connection = new(db.conn.ConnectionString))
{
   db.conn.Open();
   NpgsqlCommand command = db.conn.CreateCommand();
   command.CommandText = "SELECT user_id, firstname FROM users_table";
   NpgsqlDataReader reader = command.ExecuteReader();
   List<string?[]> data = new();
   while (reader.Read())
   {
       data.Add(new string[2]);
       data[^1][0] = reader[0].ToString();
       data[^1][1] = reader[1].ToString();
   }
   reader.Close();
   db.conn.Close();
   foreach (string?[] s in data)
   {
       try
       {
           string[] part = context.Update.Message.Text.Split(' '); 
           int lenghtPart = part[1].Length; // узнаем длину ссылки

           await context.Client.SendPhotoAsync(
           chatId: s[0]!.ToString(),
           photo: part[1], //указываем ссылку
           caption: context.Update.Message.Text[(14 + (lenghtPart+1))..], //обрезаем команду и ссылку c пробелом
           cancellationToken: cancellationToken);
       }
       catch (Exception ex)
       {
           Console.WriteLine("Error: " + ex);
           if (context.Update.Message.Type != MessageType.Photo)
           {
               await context.Client.SendTextMessageAsync(
                   1111111,
                   "Ссылка на фото не корректна. Попробуйте снова",
                   cancellationToken: cancellationToken);
           }
           else
           {
               db.conn.Open();
               NpgsqlCommand command2 = db.conn.CreateCommand();
               command2.CommandText = $"SELECT username, firstname, lastname FROM users_table WHERE user_id = {s[0]}";
               NpgsqlDataReader reader2 = command2.ExecuteReader();
               List<string?[]> data2 = new();
               while (reader.Read())
               {
                   data2.Add(new string[2]);
                   data2[^1][0] = reader2[0].ToString();
                   data2[^1][1] = reader2[1].ToString();
                   data2[^1][2] = reader2[2].ToString();
               }
               reader.Close();
               db.conn.Close();

               foreach (string?[] a in data)
               {
                   await context.Client.SendTextMessageAsync(
                       1111111,
                       $"Пользователь {a[0]} | {a[1]} | {a[2]} заблокировал бота",
                       cancellationToken: cancellationToken);
               }
           }
       }
   }

Второй способ если нужно выбрать с компьютера/телефона

var file = await context.Client.GetFileAsync(context.Update.Message.Photo.LastOrDefault().FileId);

await context.Client.SendPhotoAsync(
chatId: s[0]!.ToString(),
photo: $"{file.FileId}",
caption: context.Update.Message.Caption[14..],
cancellationToken: cancellationToken);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.