rapierorg / telegram-bot-swift

Telegram Bot SDK for Swift (unofficial)
https://github.com/rapierorg/telegram-bot-swift/wiki
Apache License 2.0
375 stars 63 forks source link

Keyboards not working #72

Closed Alexsilvacodes closed 4 years ago

Alexsilvacodes commented 5 years ago

Hi! I'm trying to use reply markups with InlineKeyboardMarkup and ReplyKeyboardMarkup, but is not working for both types.

In the first place it doesn't allow me to reach empty init, but still if I try to create buttons and markups with the initializers present (json and data), I can see that rawDictionary contains the values that I'm using, but the parameter reply_markup in the URL is sent empty and of course the buttons will not appear in the chat.

Thank you

cipi1965 commented 5 years ago

Hey, what method are you using? Could you send me a sample code?

Alexsilvacodes commented 5 years ago

Sure:

var buttons: [InlineKeyboardButton] = []
items.forEach { item in
    let buttonDict = ["text": item.name, "callback_data": "item/\(item.id)"]
    let button = InlineKeyboardButton(json: buttonDict)
    buttons.append(button)
}
var matrix: [[InlineKeyboardButton]] = [[]]
while buttons.count > 1 {
    matrix.append([buttons.removeFirst(), buttons.removeFirst()])
}
if buttons.count > 0 {
    matrix.append([buttons.removeFirst()])
}
let buttonKeyboard = ["keyboard": matrix]
let keyboard = InlineKeyboardMarkup(json: buttonKeyboard)
context.respondAsync("Something",
                    reply_to_message_id: context.message?.messageId,
                    reply_markup: keyboard)

I've tried to use InlineKeyboardButton(json: "") and InlineKeyboardMarkup(json: ""), and after that, to set each property like this:

let button = InlineKeyboardButton(json: "")
button.text = "title"

I've tried too, to use bot.sendMessage, in exchange for respondAsync. Nothing works

cipi1965 commented 5 years ago

Thank you, could you try using https://github.com/cipi1965/telegram-bot-swift.git rapier branch? I added an empty initializer to types, you should try setting properties of types

hdcola commented 4 years ago

I wrote a sample project using the Inline Keyboard, would @cipi1965 like to put it in Examples? You can see it here.

https://github.com/HDCodePractice/CityHelper/tree/master/simple-keyboard

It also really took me a bit of time, and I'm sure I would have written the code out a bit faster if there had been relevant examples or documentation in the project.

cipi1965 commented 4 years ago

Check @hdcola example, feel free to reopen this issue if you have problems