skip to Main Content

I am developing a Telegram bot using C# and I’m looking for a way to allow the bot to show a positive reaction—similar to a "like" or thumbs up—to user messages when certain conditions are met (e.g., the text has no errors). I understand that the Telegram Bot API does not support reactions in the same way a user can "like" messages. However, is there a recommended way to express similar feedback from my bot?

Currently, my bot sends a reply message with a thumbs-up emoji to commend error-free messages. Are there other, perhaps more subtle or interactive ways to achieve this within the Telegram framework, such as using inline keyboards or custom emojis?

Here’s the snippet from my C# code handling the response:

if (!response.Contains("NO CHANGES"))
{
    // Logic to handle and respond to user's text here
}
else
{
    // Logic when no changes have been made to the user's text
    await botClient.SendTextMessageAsync(chatId: chatId,
        text: $"{userName}, your text is error-free! 👍",
        replyToMessageId: messageId,
        parseMode: ParseMode.Html,
        cancellationToken: cancellationToken);
}

Any guidance or suggestions for implementing an effective feedback feature in Telegram bots would be greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    setMessageReaction

    public static async Task SendReactionAsync(long chatId, long messageId, string botToken)
    {
        using (var httpClient = new HttpClient())
        {
            var requestUrl = $"https://api.telegram.org/bot{botToken}/setMessageReaction";
            var requestData = new
            {
                chat_id = chatId,
                message_id = messageId,
                reaction = new[]
                {
                    new { type = "emoji", emoji = "👍" }
                },
                is_big = true,
            };
    
            var content = new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(requestUrl, content);
    
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Reaction sent successfully!");
            }
            else
            {
                Console.WriteLine($"Failed to send reaction: {response.StatusCode}");
            }
        }
    }
    

  2. actually, from Bot API 7.0, Telegram Bots can handle reactions just like normal users can do. So, you can implement this feature with Bot API itself.

    NB: I’m not good with C#, but I’ve gone through the code of the Bot API framework you’re possibly using and found that it supports setting reactions.

    Here’s a sample code, please let me know if it works:

    // Create the ReactionTypeEmoji instance with the 👍 emoji
    var thumbsUpReaction = new ReactionTypeEmoji
    {
        Emoji = "👍"
    };
    
    // Call the SetMessageReactionAsync method
    await botClient.SetMessageReactionAsync(
        chatId: chatId,
        messageId: yourMessageId, // Replace with your actual message ID
        reaction: new[] { thumbsUpReaction }
    );
    

    if I’m wrong with the C# code, probably this endpoint help you:

    https://api.telegram.org/bot<TOKEN>/setMessageReaction
    

    Call this API endpoint with data like:

    {
        "chat_id": 1234,
        "message_id": 123,
        "reaction": [
            {
                "type": "emoji",
                "emoji": "👍"
            }
        ]
    }
    

    Here’s setMessageReaction method’s documentation.

    Hope this helps 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search