justdmitry / NetTelegramBotApi

C# client library for building Telegram bot
MIT License
74 stars 28 forks source link

i cant use inlineKeyboard With CallBackData #46

Closed ghost closed 7 years ago

ghost commented 7 years ago

hi when i use inlineKeyboard with Url is work but when i use CallBackData it is not work and it doesnt send any message in bot? how is it? and please tell about CallBackQuery? thanks

it's my code

 var reqAction = new SendMessage(update.Message.Chat.Id, "test") { 
 ReplyMarkup = new InlineKeyboardMarkup
 {
   InlineKeyboard = new[] {
             new[] { new InlineKeyboardButton {
                             Text="test", 
                             CallbackData="1"
                            }
                       }
              }
    }
  };
 bot.MakeRequestAsync(reqAction);
 continue;
justdmitry commented 7 years ago

Does inline mode is enabled for your bot (via BotFather)?

ghost commented 7 years ago

@justdmitry yes,but i don't use inline mode and inline query i want just send simple inlineKeyboard with CallBackData i can send inlineKeyboard with Url but that's not

you can send me a Example code for it thanks

justdmitry commented 7 years ago

According to docs you should receive Update with CallbackQuery, not with Message. Did you checked CallbackQuery property?

MTare commented 7 years ago

I've the same problem as my friend saeedata said , I can easily send message with URL buttons, but when send message with Callback buttons, there is no message sent by my bot, It means the client app doesn't receive any message so the user can't push any button to send CallbackQuery to my Bot, I've debug the source and i find out the different is in MakeRequestAsync Func, when send message with URL buttons via this function, every thing is OK and response.IsSuccessStatusCode is true But when send message with Callback buttons via this function, every thing goes wrong and i have not any response. and i don't know what's the matter!!!? also inline mode is enabled for my bot

the code is here

if (text == "key")
{
    var keyb = new InlineKeyboardMarkup()
    {
        InlineKeyboard = new[] {
            new[] {new InlineKeyboardButton {Text = "TestBut" , CallbackData = "1"}}
            }
    };
    var reqAction = new SendMessage(update.Message.Chat.Id, "testcallbackbut") { ReplyMarkup = keyb};
    var mes = bot.MakeRequestAsync(reqAction);
    continue;
}

    public async Task<T> MakeRequestAsync<T>(RequestBase<T> request)
    {
        using (var client = new HttpClient(MakeHttpMessageHandler()))
        {
            client.BaseAddress = baseAddress;
            using (var httpMessage = new HttpRequestMessage(HttpMethod.Get, request.MethodName))
            {
                var postContent = request.CreateHttpContent();
                if (postContent != null)
                {
                    httpMessage.Method = HttpMethod.Post;
                    httpMessage.Content = postContent;
                }

                using (var response = await client.SendAsync(httpMessage).ConfigureAwait(false))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                        var result = DeserializeMessage<BotResponse<T>>(responseText);
                        if (result.Ok)
                        {
                            var retVal = result.Result;
                            var forPostProcessing = retVal as IPostProcessingRequired;
                            if (forPostProcessing != null)
                            {
                                forPostProcessing.PostProcess(accessToken);
                            }
                            return retVal;
                        }
                    }
                    return default(T);
                }
            }
        }
    }
justdmitry commented 7 years ago

Found it. Problem is in Json serialization settings.

I'll upload fix couple days later.

Quickfix for current 4.0.0 version:

Add this line somewhere at start of your program:

TelegramBot.JsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

MTare commented 7 years ago

Great Job, IT WORKS thanks a lot, but just a question for more information, what does this setting do? does this change affect other process or functions? Tnks in advance.

justdmitry commented 7 years ago

YES, this affect other messages that you send via bot. It expected to affect only positively - i.e., fix similar problems in other messages.

But NO, this does not affect any other function in your program (not related to bot), as soon as TelegramBot.JsonSettings is used only inside NetTelegramBotApi

justdmitry commented 7 years ago

v4.1.0 uploaded to NuGet