TelegramBots / Telegram.Bot

.NET Client for Telegram Bot API
https://telegrambots.github.io/book
MIT License
3.17k stars 686 forks source link

Repeatable Parsing Exception in SendTextMessageAsync #634

Closed dev-masih closed 6 years ago

dev-masih commented 6 years ago

Steps to reproduce

you can reproduce this bug with this tiny small project:

class Program
{
    public static TelegramBotClient botClient;
    static void Main(string[] args)
    {
        botClient = new TelegramBotClient("TOKEN");
        botClient.OnMessage += BotOnMessageReceived;
        botClient.StartReceiving();
        Console.ReadKey();
    }
    private static void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
    {
        Message message = messageEventArgs.Message;
        try
        {
            //Odd Message But Yes
            botClient.SendTextMessageAsync(message.Chat.Id, "Lang.HelpSection_Rules", ParseMode.Markdown, true, false, 0).GetAwaiter().GetResult();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Expected behavior

Bot Answers with Text "Lang.HelpSection_Rules"

Actual behavior

Exception Happens with this ex object:

{
  "ClassName": "Telegram.Bot.Exceptions.ApiRequestException",
  "Message": "Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 16",
  "Data": null,
  "InnerException": null,
  "HelpURL": null,
  "StackTraceString": "   at Telegram.Bot.TelegramBotClient.<MakeRequestAsync>d__51`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at TestConsoleApp.Program.BotOnMessageReceived(Object sender, MessageEventArgs messageEventArgs) in C:\\Users\\Masih\\Downloads\\TestConsoleApp\\TestConsoleApp\\Program.cs:line 24",
  "RemoteStackTraceString": null,
  "RemoteStackIndex": 0,
  "ExceptionMethod": "8\nMoveNext\nTelegram.Bot, Version=14.2.0.0, Culture=neutral, PublicKeyToken=null\nTelegram.Bot.TelegramBotClient+<MakeRequestAsync>d__51`1\nVoid MoveNext()",
  "HResult": -2146233088,
  "Source": "Telegram.Bot",
  "WatsonBuckets": null
}

Environment data

NuGet Package Version: 14.2.0-rc-452 (i nearly sure i hit this in 14.0.0-rc-367 too)

.NET Version: .Net Framework 4.6.2

IDE: VS2017 15.6.0

App: Console Application

karb0f0s commented 6 years ago

This is not related to lib. If you want to use markup you have to escape '_', because it used as markup token. Either you use Html markup, or not use it at all

JohnDovey commented 3 years ago

Just as an FYI for those like me who had this same error and struggled to find a solution. It is possible that you have unclosed markup in the message text you are trying to send. For example, if your message is "This is *bold* text" it will work perfectly. If you forget to close the bold marker () then it will throw this error. For example `"This is bold text"` will throw the exception.