Swiftpack.co - Package - givip/Telegrammer

SwiftyBot Banner

Telegrammer

Telegram Bot Framework written in Swift 5.1 with SwiftNIO network framework

Build Version Language Platform License

What does it do

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)
}

Documentation

Usage without Vapor

  • Use the template repository as a example https://github.com/givip/telegrammer-bot-template.git
  • Current repository contains five examples of bot implementation: https://github.com/givip/Telegrammer/tree/master/Sources/DemoEchoBot https://github.com/givip/Telegrammer/tree/master/Sources/DemoHelloBot https://github.com/givip/Telegrammer/tree/master/Sources/DemoSchedulerBot https://github.com/givip/Telegrammer/tree/master/Sources/DemoSpellCheckerBot https://github.com/givip/Telegrammer/tree/master/Sources/DemoWebhooksLocally

Usage with Vapor

Demo bots

All sample bots

  1. Add Telegram Token in Environment Variables, so, either create an environment variable:
$ export TELEGRAM_BOT_TOKEN='000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
  1. Run Bot executable scheme or
$ 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.

Requirements

Contributing

See CONTRIBUTING.md file.

Author

Givi Pataridze

pataridzegivi@gmail.com @givip

Github

link
Stars: 201

Releases

Updated CommandHandler - 2020-02-20 06:38:13

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.

Telegram Bot API 4.6 update - 2020-02-04 09:45:29

Stable Release candidate - 2019-12-30 07:01:30

  • Get rid of third party libraries
  • Using only multipart-kit from Vapor
  • Using new AsyncHTTPClient based on NIO 2.0
  • Updated to latest version of Telegram API

Telegram API 4.3, bug fixes - 2019-07-16 20:05:44

  • Added swift-log
  • Minor improvements and bug fixes

New logging - apple/swift-log - 2019-03-19 10:30:23

Implemented JobQueue, added more examples - 2019-03-18 18:23:35

Added:

  • BasicJobQueue, OnceJob, RepeatedJob, DailyJob
  • SchedulerBot example, which demonstrate JobQueue works
  • SpellCheckerBot example, which demonstrate how to use InlineKeyboardButton and CallbackQueryHandler

Support for Telegram Bot API 4.1 - 2019-03-11 15:44:27

Full support for Telegram Bot API 4.1, compatibility with swift 5.0

Fix bug for sending InlineKeyboardMarkup and MaskPosition in a multipart message - 2019-03-11 15:41:48

Workaround for memory leaked HTTPClient - 2019-03-06 14:32:31

Temporarily switched from vapor/http to URLSession

Telegram Bot API 4.0 support - 2018-08-23 13:20:35

Removed webhooksPrivateKey property - 2018-08-23 13:20:18

Removed

  • webhooksPrivateKey property from Bot.Settings struct

HandlerQueue class methods improvements - 2018-08-23 13:19:33

Fixed

  • HandlerQueue class methods improvements

First stable version - 2018-07-27 19:31:23

Changed

  • Webhooks works only with http, for https use nginx proxy or similar

Documentation additions - 2018-06-30 08:59:22

Added

  • Documentation for filters
  • CaptionEntityFilter class

New documentation for bot model, methods, classes - 2018-06-21 17:25:29

Added

  • Documentation for most of public classes

Changed

  • ruby script which generate Telegram API classes improved and now generate docs for everything

Removed

  • FoundationClient class

Message helpers, commands options - 2018-06-21 17:18:20

Added

  • ParseMode type for "Markdown" or "HTML" message text
  • Automatic Webhook deletion before starting Longpolling connection
  • Helpers reply, edit, delete for Message type

Changed

  • Now command settings are settings up with OptionSet
  • Improved HelloBot example message text

Dispatcher enhancement - 2018-06-10 14:54:33

Added

  • Convenience Bot.init(token: String) method

Changed

  • HandlerCallback signature now has Update and BotContext input params
  • Now debugMode parameter in Bot.Settings is set true by default
  • Dispatcher class made safer with special thread-safe HandlersQueue class
  • Enqueue bot updates in special concurrent queue
  • Updated README.md with super simple bot example

Removed

  • DispatchStatus enum