gpt4 book ai didi

swift - 如何使用 swift 在单个消息 Telegram Bot 中发送照片、标题和内联按钮?

转载 作者:行者123 更新时间:2023-11-30 10:34:40 28 4
gpt4 key购买 nike

This is the result that I want

func getOffers(_ page: Int, _ i: Int) {
getOfferProducts(category: [""], sort: -1, page: page) { (products) in

let keyboard = [ "inline_keyboard" : [
[
["text" : "Amazon", "url" : products[i].detailedPageURL]
]
]
]

var url = "https://api.telegram.org/bot" + apiToken + "/sendPhoto?" +
"chat_id=" + chatId +
"&caption=" + products[i].title + "\n\n" +
"🔺Prezzo iniziale: " + String(products[i].startingPrice) + " €\n" +
"🏷Prezzo attuale: " + String(products[i].price) + " €\n" +
"💰Risparmi il " + String(Double(products[i].percentOff!)) + "%\n\n" +
"►" + String(products[i].detailedPageURL) +
"&photo=" + String(products[i].largeImageURL!) +
"&reply_markup=" + keyboard

DispatchQueue.main.async {
let url = URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")
let downloadTask = URLSession.shared.dataTask(with: url!) { (data : Data?, response : URLResponse?, error : Error?) in
// Do what you want with data
}
downloadTask.resume()
}

}
}

如果我不包含参数“reply_markup”,它就可以工作(显然没有按钮)。相反,如果我尝试添加按钮,它会返回错误:

{"ok":false,"error_code":400,"description":"Bad Request: can't parse reply keyboard markup JSON object"}

最佳答案

根据this solution , reply_markup 在发送到 API 之前需要转换为 JSON 编码对象。

所以,这可以解决您的问题:

    let keyboard = [ "inline_keyboard" : [
[
["text" : "Amazon", "url" : products[i].detailedPageURL]
]
]]

guard let keyboardData = try? JSONSerialization.data(withJSONObject: keyboard, options: []) else {
return
}

var url = "https://api.telegram.org/bot" + apiToken + "/sendPhoto?" +
"chat_id=" + chatId +
"&caption=" + products[i].title + "\n\n" +
"🔺Prezzo iniziale: " + String(products[i].startingPrice) + " €\n" +
"🏷Prezzo attuale: " + String(products[i].price) + " €\n" +
"💰Risparmi il " + String(Double(products[i].percentOff!)) + "%\n\n" +
"►" + String(products[i].detailedPageURL) +
"&photo=" + String(products[i].largeImageURL!) +
"&reply_markup=" + String(data: keyboardData, encoding: String.Encoding.utf8)!

关于swift - 如何使用 swift 在单个消息 Telegram Bot 中发送照片、标题和内联按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58327475/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com