-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
421 additions
and
368 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,66 @@ | ||
# telegram-vapor-bot-lib | ||
|
||
A description of this package. | ||
### Usage | ||
|
||
vapor **TGHandlers/DefaultBotHandlers.swift** | ||
```swift | ||
import Vapor | ||
import telegram_vapor_bot_lib | ||
|
||
final class DefaultBotHandlers { | ||
|
||
static func addHandlers(app: Vapor.Application, bot: TGBotPrtcl) { | ||
defaultHandler(app: app, bot: bot) | ||
commandPingHandler(app: app, bot: bot) | ||
} | ||
|
||
private static func defaultHandler(app: Vapor.Application, bot: TGBotPrtcl) { | ||
let handler = TGMessageHandler(filters: (.all && !.command.names(["/ping"]))) { update, bot in | ||
try update.message?.reply(text: "Success", bot: bot) | ||
} | ||
bot.connection.dispatcher.add(handler) | ||
} | ||
|
||
private static func commandPingHandler(app: Vapor.Application, bot: TGBotPrtcl) { | ||
let handler = TGCommandHandler(commands: ["/ping"]) { update, bot in | ||
try update.message?.reply(text: "pong", bot: bot) | ||
} | ||
bot.connection.dispatcher.add(handler) | ||
} | ||
} | ||
|
||
``` | ||
|
||
vapor **configure.swift** | ||
|
||
```swift | ||
/// let connection: TGConnectionPrtcl = TGLongPollingConnection() | ||
let connection: TGConnectionPrtcl = TGWebHookConnection(webHookURL: "https://your_domain/some_webhook_route") | ||
TGBot.configure(connection: connection, botId: tgApi, vaporClient: app.client) | ||
try TGBot.shared.start() | ||
DefaultBotHandlers.addHandlers(app: app, bot: TGBot.shared) | ||
``` | ||
|
||
vapor **routes.swift** | ||
|
||
```swift | ||
import Vapor | ||
import telegram_vapor_bot_lib | ||
|
||
|
||
func routes(_ app: Application) throws { | ||
|
||
app.post("some_webhook_route") { (request) -> String in | ||
do { | ||
let update: TGUpdate = try request.content.decode(TGUpdate.self) | ||
try TGBot.shared.connection.dispatcher.process([update]) | ||
} catch { | ||
log.error(error.logMessage) | ||
} | ||
|
||
return "ok" | ||
} | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
// | ||
// AllFilter.swift | ||
// Telegrammer | ||
// | ||
// Created by Givi Pataridze on 21.04.2018. | ||
// | ||
// Created by Oleh Hudeichuk on 02.06.2021. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Filter for any update, said "no filter" | ||
public struct AllFilter: Filter { | ||
public class AllFilter: TGFilter { | ||
public var name: String = "all" | ||
|
||
override | ||
public func filter(message: TGMessage) -> Bool { | ||
return true | ||
} | ||
} | ||
|
||
public extension Filters { | ||
static var all = Filters(filter: AllFilter()) | ||
public extension TGFilter { | ||
static var all = AllFilter() | ||
} |
12 changes: 6 additions & 6 deletions
12
Sources/telegram-vapor-bot-lib/Bot/Filters/AudioFilter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
// | ||
// AudioFilter.swift | ||
// Telegrammer | ||
// | ||
// Created by Givi Pataridze on 21.04.2018. | ||
// | ||
// Created by Oleh Hudeichuk on 02.06.2021. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Messages that contain `Audio` | ||
public struct AudioFilter: Filter { | ||
public class AudioFilter: TGFilter { | ||
public var name: String = "audio" | ||
|
||
override | ||
public func filter(message: TGMessage) -> Bool { | ||
return message.audio != nil | ||
} | ||
} | ||
|
||
public extension Filters { | ||
static var audio = Filters(filter: AudioFilter()) | ||
public extension TGFilter { | ||
static var audio = AudioFilter() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 23 additions & 7 deletions
30
Sources/telegram-vapor-bot-lib/Bot/Filters/CommandFilter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,39 @@ | ||
// | ||
// CommandFilter.swift | ||
// Telegrammer | ||
// | ||
// Created by Givi Pataridze on 21.04.2018. | ||
// | ||
// Created by Oleh Hudeichuk on 02.06.2021. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Messages which contains command entity | ||
public struct CommandFilter: Filter { | ||
public class CommandFilter: TGFilter { | ||
|
||
public var name: String = "command" | ||
|
||
private var _names: [String]? = nil | ||
public func names(_ names: [String]) -> Self { | ||
_names = names | ||
return self | ||
} | ||
|
||
override | ||
public func filter(message: TGMessage) -> Bool { | ||
guard let entity = message.entities else { return false } | ||
return entity.contains(where: { $0.type == .botCommand }) | ||
if let names = _names { | ||
var trigger: Bool = false | ||
for name in names { | ||
if message.contains(command: name) { | ||
trigger = true | ||
} | ||
} | ||
return entity.contains(where: { $0.type == .botCommand }) && trigger | ||
} else { | ||
return entity.contains(where: { $0.type == .botCommand }) | ||
} | ||
} | ||
} | ||
|
||
public extension Filters { | ||
static var command = Filters(filter: CommandFilter()) | ||
public extension TGFilter { | ||
static var command = CommandFilter() | ||
} |
12 changes: 6 additions & 6 deletions
12
Sources/telegram-vapor-bot-lib/Bot/Filters/ContactFilter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
// | ||
// ContactFilter.swift | ||
// Telegrammer | ||
// | ||
// Created by Givi Pataridze on 21.04.2018. | ||
// | ||
// Created by Oleh Hudeichuk on 02.06.2021. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Messages that contain `Contact` | ||
public struct ContactFilter: Filter { | ||
public class ContactFilter: TGFilter { | ||
|
||
public var name: String = "contact" | ||
|
||
override | ||
public func filter(message: TGMessage) -> Bool { | ||
return message.contact != nil | ||
} | ||
} | ||
|
||
public extension Filters { | ||
static var contact = Filters(filter: ContactFilter()) | ||
public extension TGFilter { | ||
static var contact = ContactFilter() | ||
} |
12 changes: 6 additions & 6 deletions
12
Sources/telegram-vapor-bot-lib/Bot/Filters/DocumentFilter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
// | ||
// DocumentFilter.swift | ||
// Telegrammer | ||
// | ||
// Created by Givi Pataridze on 21.04.2018. | ||
// | ||
// Created by Oleh Hudeichuk on 02.06.2021. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Messages that contain `Document` | ||
public struct DocumentFilter: Filter { | ||
public class DocumentFilter: TGFilter { | ||
|
||
public var name: String = "document" | ||
|
||
override | ||
public func filter(message: TGMessage) -> Bool { | ||
return message.document != nil | ||
} | ||
} | ||
|
||
public extension Filters { | ||
static var document = Filters(filter: DocumentFilter()) | ||
public extension TGFilter { | ||
static var document = DocumentFilter() | ||
} |
15 changes: 8 additions & 7 deletions
15
Sources/telegram-vapor-bot-lib/Bot/Filters/EntityFilter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
// | ||
// EntityFilter.swift | ||
// Telegrammer | ||
// | ||
// Created by Givi Pataridze on 21.04.2018. | ||
// | ||
// Created by Oleh Hudeichuk on 02.06.2021. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Filters messages to only allow those which have a `MessageEntity` where their type matches `type`. | ||
public struct EntityFilter: Filter { | ||
public class EntityFilter: TGFilter { | ||
|
||
let entityTypes: Set<TGMessageEntityType> | ||
|
||
public init(types: [TGMessageEntityType]) { | ||
self.entityTypes = Set(types) | ||
super.init() | ||
} | ||
|
||
public var name: String = "entity" | ||
|
||
override | ||
public func filter(message: TGMessage) -> Bool { | ||
guard let entities = message.entities else { return false } | ||
let incomingTypes = entities.map { $0.type } | ||
return !entityTypes.isDisjoint(with: incomingTypes) | ||
} | ||
} | ||
|
||
public extension Filters { | ||
static func entity(types: [TGMessageEntityType]) -> Filters { | ||
return Filters(filter: EntityFilter(types: types)) | ||
public extension TGFilter { | ||
static func entity(types: [TGMessageEntityType]) -> TGFilter { | ||
return EntityFilter(types: types) | ||
} | ||
} |
Oops, something went wrong.