Jeffrey Willis
Jeffrey Willis

Reputation: 45

sendphotoasync() from hard drive

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;
                }
            }        
        }
    }
}

Upvotes: 1

Views: 1827

Answers (1)

Ygalbel
Ygalbel

Reputation: 5519

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

Upvotes: 3

Related Questions