Telegram Bot Framework written in Swift 5.1 with SwiftNIO network framework
Telegrammer is open-source framework for Telegram Bots developers. It was built on top of Apple/SwiftNIO
Join to our Telegram developers chat Join to our Telegrammer channel on Vapor Discord server
The simplest code of Echo Bot looks like this:
main.swift
import Foundation
import Telegrammer
do {
let bot = try Bot(token: "BOT_TOKEN_HERE")
let echoHandler = MessageHandler { (update, _) in
_ = try? update.message?.reply(text: "Hello \(update.message?.from?.firstName ?? "anonymous")", from: bot)
}
let dispatcher = Dispatcher(bot: bot)
dispatcher.add(handler: echoHandler)
_ = try Updater(bot: bot, dispatcher: dispatcher).startLongpolling().wait()
} catch {
exit(1)
}
$ export TELEGRAM_BOT_TOKEN='000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
$ swift run
EchoBot sources Starts/stops with command "/echo", then simply responds with your message
HelloBot sources Says "Hello" to new users in group. Responds with "hello" message on command "/greet"
SchedulerBot sources Demonstrate Jobs Queue scheduling mechanism. Command "/start X" starts repeatable job, wich will send you a message each X seconds. Command "/once X" will send you message once after timeout of X seconds. Command "/stop" stops JobsQueue only for you. Other users continues to receive scheduled messages.
SpellCheckerBot sources Demonstrate how works InlineMenus and Callback handlers. Command "/start" will start bot. Send any english text to bot and it will be checked for mistakes. Bot will propose you some fixes in case of found mistake.
See CONTRIBUTING.md file.
Givi Pataridze
link |
Stars: 289 |
Last commit: 2 years ago |
In a group chat, Telegram allows specifying the bot to receive commands sent by a user by appending "@botUsername" in the command, while this feature is not working as expected currently in Telegrammer:
If the user specifies the bot, the CommandHandler considers it a different command with "@" in the command name. We need to manually add a new command "/originalCommand@botUsername" to support it, which could be somehow misleading since most users (at least me) of this package would normally consider this automatically handled.
Using the workaround in 1. would cause each command to be coded twice, which can be tedious and inelegant in cases where there are many commands in a CommandHandler. Therefore, I added the ability of checking the bot specified to the CommandHandler. This change does not break the existing API but only asks the username of the bot in the initializer optionally, and should then handle things correctly as intended.
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics