Skip to content

Commit

Permalink
change filters + add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzh committed Jun 1, 2021
1 parent f229fe5 commit 86ed914
Show file tree
Hide file tree
Showing 36 changed files with 421 additions and 368 deletions.
65 changes: 64 additions & 1 deletion README.md
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"
}
}
```


12 changes: 6 additions & 6 deletions Sources/telegram-vapor-bot-lib/Bot/Filters/AllFilter.swift
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 Sources/telegram-vapor-bot-lib/Bot/Filters/AudioFilter.swift
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()
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
//
// CaptionEntity.swift
// Telegrammer
//
// Created by Givi Pataridze on 24/06/2018.
//
// Created by Oleh Hudeichuk on 02.06.2021.
//

import Foundation

/// Filters media messages to only allow those which have a `MessageEntity` where their type matches `type`.
public struct CaptionEntityFilter: Filter {
public class CaptionEntityFilter: TGFilter {

var entityType: TGMessageEntityType

public init(type: TGMessageEntityType) {
self.entityType = type
super.init()
}

public var name: String = "caption_entity"

override
public func filter(message: TGMessage) -> Bool {
guard let entities = message.entities else { return false }
return entities.contains(where: { (entity) -> Bool in
Expand All @@ -26,8 +27,8 @@ public struct CaptionEntityFilter: Filter {
}
}

public extension Filters {
static func captionEntity(type: TGMessageEntityType) -> Filters {
return Filters(filter: CaptionEntityFilter(type: type))
public extension TGFilter {
static func captionEntity(type: TGMessageEntityType) -> TGFilter {
return CaptionEntityFilter(type: type)
}
}
15 changes: 8 additions & 7 deletions Sources/telegram-vapor-bot-lib/Bot/Filters/ChatFilter.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
//
// ChatFilter.swift
// Telegrammer
//
// Created by Givi Pataridze on 21.04.2018.
//
// Created by Oleh Hudeichuk on 02.06.2021.
//

import Foundation

/// Filters messages to allow only those which are from specified chat ID.
public struct ChatFilter: Filter {
public class ChatFilter: TGFilter {

var chatId: Int64
var username: String?

public init(chatId: Int64, username: String? = nil) {
self.chatId = chatId
self.username = username
super.init()
}

public var name: String = "chat"

override
public func filter(message: TGMessage) -> Bool {
guard message.chat.id == chatId else { return false }
guard let desiredUsername = username else { return true }
Expand All @@ -28,8 +29,8 @@ public struct ChatFilter: Filter {
}
}

public extension Filters {
static func chat(chatId: Int64, username: String? = nil) -> Filters {
return Filters(filter: ChatFilter(chatId: chatId, username: username))
public extension TGFilter {
static func chat(chatId: Int64, username: String? = nil) -> TGFilter {
return ChatFilter(chatId: chatId, username: username)
}
}
30 changes: 23 additions & 7 deletions Sources/telegram-vapor-bot-lib/Bot/Filters/CommandFilter.swift
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 Sources/telegram-vapor-bot-lib/Bot/Filters/ContactFilter.swift
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 Sources/telegram-vapor-bot-lib/Bot/Filters/DocumentFilter.swift
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 Sources/telegram-vapor-bot-lib/Bot/Filters/EntityFilter.swift
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)
}
}
Loading

0 comments on commit 86ed914

Please sign in to comment.