.package(name: "telegram-vapor-bot", url: "https://github.com/nerzh/telegram-vapor-bot", .upToNextMajor(from: "1.2.4")),
.product(name: "telegram-vapor-bot", package: "telegram-vapor-bot"),
import telegram_vapor_bot
let tgApi: String = "XXXXXXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" // The received token via botfather
let connection: TGConnectionPrtcl = TGLongPollingConnection()
TGBot.configure(connection: connection, botId: tgApi, vaporClient: app.client)
try TGBot.shared.start()
/// set level of debug if you needed
TGBot.log.logLevel = .error
DefaultBotHandlers.addHandlers(app: app, bot: TGBot.shared)
let near = TGMessageHandler(filters: (.command.names([Keys.near, Keys.start]))) { update, bot in
guard let userID = update.message?.from?.id else { fatalError("userID not found") }
// The button that asks for the location
let nearButton: [[TGKeyboardButton]] = [[TGKeyboardButton(text: "Near Playgrounds", requestContact: false, requestLocation: true, requestPoll: nil)]]
let nearKeyboard: TGReplyKeyboardMarkup = .init(keyboard: nearButton, resizeKeyboard: true)
let nearParams: TGSendMessageParams = .init(chatId: .chat(userID), text: "Specify your location", replyMarkup: .replyKeyboardMarkup(nearKeyboard))
try bot.sendMessage(params: nearParams)
}
var buttons: [[TGInlineKeyboardButton]] = []
for playground in playgrounds {
let title = playground.about.title + ". " + playground.about.place.address
buttons.append([.init(text: title, callbackData: "\(Keys.playground)|\(playground.id!)")])
}
let keyboard: TGInlineKeyboardMarkup = .init(inlineKeyboard: buttons)
let params: TGSendMessageParams = .init(chatId: .chat(userID), text: "Nearest playgrounds", replyMarkup: .inlineKeyboardMarkup(keyboard))
try bot.sendMessage(params: params)
//MARK: Responses to the game
let followsHandler = TGCallbackQueryHandler(pattern: Keys.follows) { update, bot in
guard let chatID = update.callbackQuery?.message?.chat.id else { fatalError("user id not found") }
if let data = update.callbackQuery?.data {
let values = data.split(separator: "|")
if (values.count == 2) {
if let gameID = UUID(uuidString: String(values[1])) {
var text = ""
let _ = try self.follows(gameID: gameID, app: app).throwingMap { follows in
for (ind, follow) in follows.enumerated() {
text += follow.user.username
if (ind != follows.count - 1) { text += "\n" }
}
var buttons: [[TGInlineKeyboardButton]] = []
buttons.append([
.init(text: "Respond", callbackData: "\(Keys.follow)|\(gameID)")
])
let keyboard: TGInlineKeyboardMarkup = .init(inlineKeyboard: buttons)
let params: TGSendMessageParams = .init(chatId: .chat(chatID),
text: text,
replyMarkup: .inlineKeyboardMarkup(keyboard))
try bot.sendMessage(params: params)
}
}
}
}
}
bot.connection.dispatcher.add(followHandler)
static func follows(gameID: UUID, app: Vapor.Application) throws -> EventLoopFuture<[Follow]> {
return Game.find(gameID, on: app.db).unwrap(or: Abort(.notFound)).flatMap { game in
return game.$follows.get(on: app.db).flatMap { follows in
return follows.map { follow in
return follow.$user.get(on: app.db).map { _ in }
}.flatten(on: app.eventLoopGroup.next()).map { _ in
return follows
}
}
}
}
try bot.sendInvoice(params: TGSendInvoiceParams(chatId: .chat(userID), title: "iPhone 13", description: "The iPhone is a line of smartphones designed and marketed by Apple Inc. that use Apple's iOS mobile operating system", payload: "Your payload", providerToken: "381762222:TEST:22222", currency: "RUB", prices: [TGLabeledPrice(label: "₽", amount: 100_000)]))
//MARK: Processing payments
bot.connection.dispatcher.addBeforeAllCallback { updates, some in
for update in updates {
if let preCheckoutQuery = update.preCheckoutQuery {
// We check the payment and send the solution. It must be done in 10 seconds
try bot.answerPreCheckoutQuery(params: TGAnswerPreCheckoutQueryParams(preCheckoutQueryId: preCheckoutQuery.id, ok: true, errorMessage: nil))
}
}
try some(updates)
}