skip to Main Content

I want to send a photo using the Telegram.Bot library, but it’s not working!

Here is my code:

private void btnSendImage_Click(object sender, RoutedEventArgs e)
    {
        var Bot = new Telegram.Bot.Api(token);

        Task<Telegram.Bot.Types.Update[]> res = Bot.GetUpdates();

        List<string> users = GetIds();
        foreach (var update in res.Result)
        {
            if (!users.Contains("" + update.Message.Chat.Id))
            {
                AddId("" + update.Message.Chat.Id);

            }
        }
        users = GetIds();
        foreach (var item in users)
        {
            if (item.Length > 0)
            {

                var rep = Bot.SendPhoto(Convert.ToInt32(item), txtImagePath.Text, txtMessage.Text);
            }
        }

    }

and txtImagePath.text= "D:ProjectsTelegram BotTelegram BotbinDebug4.jpg";

I tested it with Bot.SendMessage and it worked fine, but I can’t send a photo!

2

Answers


  1. you need to pass a Stream to the function if you want to send an new image.

    using (var stream = File.Open(txtImagePath.Text, FileMode.Open))
    {
        var rep = await Bot.SendPhoto(Convert.ToInt32(item), stream, txtMessage.Text);
    }
    
    Login or Signup to reply.
  2. I used this code and it’s worked!

    var FileUrl = @"C:\Users\User\Documents\20160201_204055.jpg";
    using (var stream = System.IO.File.Open(FileUrl, FileMode.Open))
            {
                FileToSend fts = new FileToSend();
                fts.Content = stream;
                fts.Filename = FileUrl.Split('\').Last();
                var test = await bot.SendPhoto("@channel Name or chat_id", fts, "My Text");
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search