Swiftpack.co - Package - ShaneQi/ZEGBot


Build Status Swift Version Platforms License

This library wraps the JSON decoding processing, making it easy to decode incoming JSON String to manipulatable objects.

This library wraps the processing of converting objects to Telegram Bot API request parameters and the processing of performing request, making it easy to handle incoming update.


Add this project as a dependency in your Package.swift file.

.package(url: "https://github.com/shaneqi/ZEGBot.git", from: Version(4, 0, 0))

Quick Start

Checkout the example here: ./Example.

Or you can just put the following code into main.swift of your project.

import ZEGBot

//  Don't forget to fill in your bot token.
let bot = ZEGBot(token: "TYPE YOUR TOKEN HERE")

bot.run { update, bot in       
  //  Handle updates here...



  • Get text content of the incoming message:

    if let text = update.message?.text {
      // Do something.
  • Get other type of content if exists:

    if let voice = update.message?.voice {
      // Do something.
  • Send a text message to a chat:

    if let message = update.message {
      bot.send(message: "bar", to: message.chat)
  • Send a silent message with a none-preview markdown link to a chat:

    if let message = update?.message {
      bot.send(message: "[Google](https://google.com)", to: message.chat, parseMode: .markdown, disableWebPagePreview: true, disableNotification: true)
  • In all the sending methods, send to a Message Object means reply to this specific message. While Send to a Chat Object means send to this chat without replying to anyone.

    if let message = update?.message {
      /* This sends a message replying to another message. */
      bot.send(message: "bar", to: message)
      /* This doesn't reply to a message. */
      bot.send(message: "bar", to: message.chat)
  • Send a text message to a channel:

    if let channelPost = update?.channelPost {
      bot.send(message: "bar", to: channelPost.chat)

Support Types

  • Update
  • Message
  • Chat
  • User
  • MessageEntity
  • Audio
  • Document
  • PhotoSize
  • Sticker
  • Video
  • Voice
  • Contact
  • Location
  • Venue
  • File

Not all the types are supported, checkout more details on Telegram Bot API.

Support Methods

  • sendMessage
  • forwardMessage
  • sendPhoto
  • sendAudio
  • sendDocument
  • sendSticker
  • sendVideo
  • sendVoice
  • sendLocation
  • sendVenue
  • sendContact
  • sendChatAction
  • getFile

Not all the methods are supported, checkout more details on Telegram Bot API.


This project is licensed under Apache License v2.0.


Stars: 40
Help us keep the lights on



4.0.1 - Jun 5, 2018

4.0.0 - Sep 30, 2017

Thanks to Swift 4.0, we have this shining new Codable feature. And I love Codable!

I removed the last dependency IBM-Swift/SwiftyJSON, embraced Codable, and we don't have any dependencies anymore!

The only API change is:

UpdateHandler now handles Result<Update>. Which means that not only updates but also errors will be passed to the handler.

2.1.0 - Jul 23, 2017

In this version, network framework is replaced with URLSession of Swift core foundation. (PerfectCURL was used previously)

Under the hood, it also began using Dispatch framework in thread handling. And I think it's a good practice to use Dispatch framework if you want to do things like multithreads.

2.0.1 - Feb 27, 2017

2.0.0 - Oct 13, 2016