42 lines
1.5 KiB
Swift
42 lines
1.5 KiB
Swift
import Vapor
|
|
import Crypto
|
|
@preconcurrency import SwiftTelegramSdk
|
|
|
|
final class DefaultBotHandlers {
|
|
static func addHandlers(_ bot: TGBot) async {
|
|
await defaultBaseHandler(bot)
|
|
}
|
|
|
|
private static func defaultBaseHandler(_ bot: TGBot) async {
|
|
await botActor.bot.dispatcher.add(TGBaseHandler({ update in
|
|
guard let message = update.message else { return }
|
|
let params: TGSendMessageParams = .init(chatId: .chat(message.chat.id), text: """
|
|
Help message
|
|
""")
|
|
try await bot.sendMessage(params: params)
|
|
}))
|
|
}
|
|
|
|
private static func sendButtonOfAuth(_ bot: TGBot) async throws {
|
|
await bot.dispatcher.add(TGCallbackQueryHandler(pattern: "Subscribe", { update in
|
|
bot.log.info("press")
|
|
let userId = update.callbackQuery!.from.id
|
|
|
|
let arrayBytesOfId = withUnsafeBytes(of: userId, Array.init)
|
|
let hashedUserIdDigest = SHA512.hash(Data(arrayBytesOfId))
|
|
// Convert Digest to hex string
|
|
let hashedUserId = Data(hashedUserIdDigest).map { String(format: "%02x", $0) }.joined()
|
|
|
|
try? Database.saveUser(hashedUserId, "", false)
|
|
|
|
let params: TGAnswerCallbackQueryParams = .init(
|
|
callbackQueryId: update.callbackQuery?.id ?? "0",
|
|
text: update.callbackQuery?.data ?? "data not exit",
|
|
showAlert: nil,
|
|
url: nil,
|
|
cacheTime: nil
|
|
)
|
|
try await bot.answerCallbackQuery(params: params)
|
|
}))
|
|
}
|
|
}
|