skip to Main Content

Hi I want to send a photo with my telegram bot but my VS doesn’t recognize “FileToSend” and my error is :

int chatId = int.Parse(dgReport.CurrentRow.Cells[0].Value.ToString());
FileStream imageFile = System.IO.File.Open(txtFilePath.Text,FileMode.Open);

bot.SendPhotoAsync(chatId, new FileToSend("1234.jpg", imageFile), txtmessage.Text);

CS0246 The type or namespace name ‘FileToSend’ could not be found (are
you missing a using directive or an assembly reference?)

3

Answers


  1. Sounds like you are very new to Visual Studio and also C# so I’ll share a general tip. If you see a red squiggly line in Visual Studio you have a compile error. Your program will not build/run until it is fixed.

    Here’s my tip. Find the code that is causing the compile error (the text with a red squiggle beneath it). Right click the text and choose “Quick Actions and Refactoring”. You will see several suggestions that may fix the issue.

    The problem you are describing can usually be fixed using this feature. One of the options may be something like “Using Telegram.Bot;”. If you choose it, it will automatically put the using statement at the top of your file and fix the compilation error. This is definitely the #1 reason I use Quick Actions and Refactoring.

    enter image description here

    Login or Signup to reply.
  2. Actually, the question is quite OK. I think you just might be mixing up examples you’ve found and tried, which – if so – is an honest mistake. The FileToSend() function is related to a (deprecated) project on GitHub at ScottRFrost/TelegramBot.

    The rest of your sample seems to be mixed up a bith with, I assume, the TelegramBots/Telegram.Bot package. If you use that, the following sample might help you a bit further:

    using System.IO;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Telegram.Bot;
    using Xunit;
    
    namespace StackOverflowSupport
    {
        public class Tests
        {
            [Fact]
            public async Task SendFileWithTelegramBot()
            {
                // Requires NuGet package `Telegram.Bot` (v15.0.0)
                var token = "YOUR_TOKEN";
    
                using (var http = new HttpClient())
                {
                    int chatId = 42;
                    var imageFile = File.Open("filepath", FileMode.Open);
    
                    var bot = new TelegramBotClient(token, http);
                    await bot.SendPhotoAsync(chatId, photo: imageFile, caption: "This is a Caption");
                }
            }
        }
    }
    

    As you can see, no FileToSend() in there, which might be the cause of your problem.

    Note that this is just to help you in the right direction; it is not meant as code to be used in production. Especially reading out the stream through File.Open could be improved.

    Login or Signup to reply.
  3. The FileToSend() function is removed, use InputOnlineFile():

    FileStream imageFile = System.IO.File.Open(txtFilePath.Text,FileMode.Open);
    bot.SendPhotoAsync(chatId, new InputOnlineFile(imageFile, "image.png"), txtmessage.Text);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search