Swiftpack.co - Package - ShaneQi/ZEGBot

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.

Installation

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

.package(url: "https://github.com/shaneqi/ZEGBot.git", from: Version(4, 2, 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")

do {
  try bot.run { update, bot in       
    //  Handle updates here...
  })
} catch let error {
  NSLog("Bot exit due to: \(error)") 
}

Usage

  • 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 {
      do {
        try bot.send(message: "bar", to: message.chat)
      } catch let error {
        NSLog("Failed to send message due to: \(error)")
      }
    }
    ...
    
  • Send a silent message with a none-preview markdown link to a chat:

    ...
    if let message = update?.message {
      do {
        try bot.send(message: "[Google](https://google.com)", to: message.chat, parseMode: .markdown, disableWebPagePreview: true, disableNotification: true)
      } catch let error {
        NSLog("Failed to send message due to: \(error)")
      }
    }
    ...
    
  • 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 {
      do {
        /* This sends a message replying to another message. */
        try bot.send(message: "bar", to: message)
        /* This doesn't reply to a message. */
        try bot.send(message: "bar", to: message.chat)
      } catch let error {
        NSLog("Failed to send message due to: \(error)")
      }
    }
    ...
    
  • Send a text message to a channel:

    ...
    if let channelPost = update?.channelPost {
      do {
        try bot.send(message: "bar", to: channelPost.chat)
      } catch let error {
        NSLog("Failed to send message due to: \(error)")
      }
    }
    ...
    
    

Support Types

  • Update
  • Message
  • Chat
  • User
  • MessageEntity
  • Audio
  • Document
  • PhotoSize
  • Sticker
  • Video
  • Voice
  • Contact
  • Location
  • Venue
  • File
  • ParseMode
  • ChatAction
  • ChatMember
  • InlineKeyboardButton
  • InlineKeyboardMarkup
  • CallbackQuery

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
  • deleteMessage
  • getChatAdministrators
  • answerCallbackQuery
  • restrictChatMember
  • kickChatMember

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

License

This project is licensed under Apache License v2.0.

Github

link
Stars: 41
Help us keep the lights on

Dependencies

Releases

4.2.0 - Nov 9, 2018

API change: methods that return Result<T> objects are now changed to throws -> T.

New supported methods:

  • deleteMessage
  • getChatAdministrators
  • answerCallbackQuery
  • restrictChatMember
  • kickChatMember

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