1

It would be more convenient for me to use the file from a computer, rather than download it from a specific link.

How i can send a picture from hard driver with using sendphotoasync in Telegram.Bot?

using System;
using Telegram.Bot;
using System.IO;

namespace iBot
{
    class Program
    {

        public static DateTime timeReplacement = DateTime.Parse(Config.TimeReplacement);
        private static TelegramBotClient client;

        public static void Main()
        {       

            client = new TelegramBotClient(Config.ApiToken);

           
            for(int i = 0; ; i++)
            {
                DateTime nowTime = DateTime.Now;
                var date = DateTime.Now.ToShortDateString();


                if (nowTime.ToShortTimeString() == timeReplacement.ToShortTimeString())
                {
                    client.SendPhotoAsync(Config.ChatId, Config.Link, $"Replacements: {date}"); 
                    client.StopReceiving();
                    break;
                }
            }        
        }
    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

1 Answers1

3

The function you are using to send a picture is SendPhotoAsync and have this signature: source

    Task<Message> SendPhotoAsync(
            ChatId chatId,
            InputOnlineFile photo,
            string caption = default,
            ParseMode parseMode = default,
            IEnumerable<MessageEntity> captionEntities = default,
            bool disableNotification = default,
            int replyToMessageId = default,
            bool allowSendingWithoutReply = default,
            IReplyMarkup replyMarkup = default,
            CancellationToken cancellationToken = default
            );

That means the picture is an instance of type InputOnlineFile.

I checked InputOnlineFile class

  public InputOnlineFile(Stream content)
            : this(content, default)
        {
        }

And it gets a Stream as parameter.

You easily create a stream from file with this snippet:

FileStream fsSource = new FileStream(pathSource,
            FileMode.Open, FileAccess.Read)

(Just don't forget to dispose it after usage.)

So, the full snippet is like this:

FileStream fsSource = new FileStream(pathSource,
            FileMode.Open, FileAccess.Read)
InputOnlineFile file = new InputOnlineFile(fsSource);

... call here to SendPhotoAsync
Ygalbel
  • 5,214
  • 1
  • 24
  • 32