It's about Telegrammer, Telegram Bot framework for Linux / macOS , fully written in Swift 4.1

Exposure: How did such a thought come to mind?
One of my pet-project (iOS application related to Telegram, but now it's not about it) needed a web interface for the content manager to create a description, tag and so on. Since the application was already ideologically connected with Telegram, I immediately had the idea to send the content directly to the instant messenger and there perform the above actions.
Send such data, as it turned out, it is possible through bots (how bots are arranged in Telegram ).
Outset: I was looking for a ready-made solution, I did not find a suitable one.
And here all the stars are lined up, now you will understand what I mean ...
There are many reliable, proven, Server Side frameworks, Java, Go, Python, PHP and others that already allow you to create a bot, without any problems. But this is not about us (witnesses Jehovah's Apple), we are not looking for easy ways.
And what if to write a bot on Swift?
Quite recently (by the standards of programming languages), the Server Side Swift community began to develop actively, and several frameworks such as Vapor, Perfect, Kitura, which managed to gain sufficient fame, appeared.
Plus, Apple tossed firewood into the firebox, making life easier for developers with the low-level and high-performance framework SwiftNIO
What is cool SwiftNIO?SwiftNIO is a cross-platform, high-performance, high-performance protocol-wide network protocol.
It's like netty , but written for Swift.
SwiftNIO is a fundamentally a tool for building high-performance networking applications in Swift. It is inefficient or untenable. This is a very limited number of relatively low-utilization connections, such as HTTP servers.
To achieve your goals SwiftNIO extensively uses "non-blocking I / O": hence the name! I / O I / O I / O be performed without waiting
SwiftNIO doesn’t aim to provide high-level solutions like, for example, web frameworks do. Instead, SwiftNIO is focused on providing low-level building blocks. It is direct to the Swift ecosystem. However, it is possible to use their web frameworks.
What I considered from the ready Telegram Bot Swift libraries:
zmeyc / telegram-bot-swift - A fairly advanced library, the entire API Telegram Bot parses from the site, both methods and models, advanced routing, works on both macOS and Linux.
The disadvantages associated with the fact that it was written for a long time, and at that time, all Apple frameworks (especially the Foundation) worked on Linux very unstable:
- Sends requests through curl
- At the time of the study, both the library itself and the dependent libraries were not ported to swift 4
- WebHooks not supported
- The repository has not been updated for a long time, as a result, some chips of the updated Telegram Bot API are not supported.
FabrizioBrancati / SwiftyBot - May the author forgive me, I don’t understand why this bot has the most stars, the obsolete Vapor 2.4.0 under the hood, and the lone main.swift, which simply shows the simplest work with the bot. No models, helpers, sending queues, nothing. Oh no! Unlike competitors, it supports WebHooks.
ShaneQi / ZEGBot - Very simple implementation, implemented the basic methods, models, and again only LongPolling.
So, it is decided, we will write our Framework. Frankly, I was inspired by Andrey Fidrya with my bot, but I decided that I could try to do better.
The climax: Welcome to the world of backend. SwiftNIO, thanks for the blown-up brain.
I admit, after iOS applications, it is quite difficult to write an application for Server Side, you have to think differently.
By the time the framework was started, the guys from Vapor announced the beta and already on the basis of SwiftNIO. Vapor has a modular architecture, each layer lives in its own repository, which is very convenient; you can only use part of the implementations.
I decided to use:
Along the way, some bugs were found in Vapor HTTPClient, successfully fixed, HTTPServer was improved, to work with https out of the box, without having to use nginx and the like.
What happened:
- The bot API was designed with a strong eye on the proven player in this area. Python-telegram-bot
- Like zmeyc / telegram-bot-swift, all models and methods of the Telegram Bot API bot can be generated by running the script.
- Implemented Longpolling and WebHooks modes
- Handlers implemented: CommandHandler, CallbackQueryHandler, RegexpHandler, MessageHandler
- Many filters for updates
- Two simple bots, for example, standard EchoBot and HelloBot
Decoupling: We write a bot, generally useful, let him check the spelling.
Bot: @yandex_spell_checker_bot
Source code: https://github.com/givip/YandexSpellCheckerBot
main.swift looks like this:
import Foundation import Telegrammer /// enviroment variable ( , ) guard let token = Enviroment.get("SPELL_CHECKER_BOT_TOKEN") else { exit(1) } do { /// let bot = try Bot(token: token) /// let dispatcher = Dispatcher(bot: bot) /// let controller = SpellCheckerController(bot: bot) /// /start, let commandHandler = CommandHandler(commands: ["/start"], callback: controller.start) dispatcher.add(handler: commandHandler) /// , let textHandler = MessageHandler(filters: .private, callback: controller.spellCheck) dispatcher.add(handler: textHandler) /// , . let inlineHandler = CallbackQueryHandler(pattern: "\\w+", callback: controller.inline) dispatcher.add(handler: inlineHandler) /// Longpolling _ = try Updater(bot: bot, dispatcher: dispatcher).startLongpolling().wait() } catch { print(error.localizedDescription) }
Epilogue: iOS developers and not only, use!
The framework is at the beta stage, and anyone can use it for their needs.
After a month of testing and improvements, there were some flaws, Wishlist and many enhancement issues, but nevertheless, "Spell Checker" has been running on Ubuntu for a long time.
I would be grateful for any feedback, and throwing on the fan.