Skip to content

Commit

Permalink
Added check for existing webhooks certificate and removed private cer…
Browse files Browse the repository at this point in the history
…t property as unnesessary
  • Loading branch information
givip committed Aug 20, 2018
1 parent 7d22b1a commit b282140
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
1 change: 0 additions & 1 deletion Sources/EchoBot/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var settings = Bot.Settings(token: token)
//settings.webhooksUrl = Enviroment.get("TELEGRAM_BOT_WEBHOOK_URL")!
//settings.webhooksPort = Int(Enviroment.get("TELEGRAM_BOT_PORT")!)!
//settings.webhooksPublicCert = Enviroment.get("TELEGRAM_BOT_PUBLIC_KEY")!
//settings.webhooksPrivateKey = Enviroment.get("TELEGRAM_BOT_PRIVATE_KEY")!

let bot = try! Bot(settings: settings)

Expand Down
1 change: 0 additions & 1 deletion Sources/HelloBot/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var settings = Bot.Settings(token: token, debugMode: true)
//settings.webhooksUrl = Enviroment.get("TELEGRAM_BOT_WEBHOOK_URL")!
//settings.webhooksPort = Int(Enviroment.get("TELEGRAM_BOT_PORT")!)!
//settings.webhooksPublicCert = Enviroment.get("TELEGRAM_BOT_PUBLIC_KEY")!
//settings.webhooksPrivateKey = Enviroment.get("TELEGRAM_BOT_PRIVATE_KEY")!

let bot = try! Bot(settings: settings)

Expand Down
1 change: 0 additions & 1 deletion Sources/Telegrammer/Bot/Bot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public final class Bot {
public var webhooksUrl: String? = nil
public var webhooksPort: Int? = nil
public var webhooksPublicCert: String? = nil
public var webhooksPrivateKey: String? = nil

public init(token: String, debugMode: Bool = true) {
self.token = token
Expand Down
17 changes: 10 additions & 7 deletions Sources/Telegrammer/Network/Webhooks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ class Webhooks: Connection {
public func start() throws -> Future<Void> {
guard let ip = bot.settings.webhooksIp,
let url = bot.settings.webhooksUrl,
let port = bot.settings.webhooksPort,
let publicCert = bot.settings.webhooksPublicCert else {
let port = bot.settings.webhooksPort else {
throw CoreError(identifier: "Webhooks",
reason: "Initialization parameters wasn't found in enviroment variables")
}

guard let fileHandle = FileHandle(forReadingAtPath: publicCert) else {
let errorDescription = "Public key '\(publicCert)' for HTTPS server wasn't found"
Log.error(errorDescription)
throw CoreError(identifier: "FileIO", reason: errorDescription)
var cert: InputFile? = nil

if let publicCert = bot.settings.webhooksPublicCert {
guard let fileHandle = FileHandle(forReadingAtPath: publicCert) else {
let errorDescription = "Public key '\(publicCert)' was specified for HTTPS server, but wasn't found"
Log.error(errorDescription)
throw CoreError(identifier: "FileIO", reason: errorDescription)
}
cert = InputFile(data: fileHandle.readDataToEndOfFile(), filename: publicCert)
}

let cert = InputFile(data: fileHandle.readDataToEndOfFile(), filename: publicCert)
let params = Bot.SetWebhookParams(url: url, certificate: cert, maxConnections: maxConnections, allowedUpdates: nil)
return try bot.setWebhook(params: params).flatMap { (success) -> Future<Void> in
Log.info("setWebhook request result: \(success)")
Expand Down

0 comments on commit b282140

Please sign in to comment.