Swiftpack.co - Swiftgram/TDLibKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Swiftgram/TDLibKit
Native Swift wrapper for Telegram's TDLib. Available on iOS, macOS, watchOS and tvOS.
.package(url: "https://github.com/Swiftgram/TDLibKit.git", from: "1.0.0-tdlib-1.7.8-bbae7be4")

TDLibKit

CI

TDLibKit is a native Swift wrapper for TDLib with support for iOS, macOS, watchOS and even tvOS.

Powered by pre-built multi-platform TDLibFramework implementation of TDLib and generated sources with tl2swift

Installation

Xcode

  1. Install Xcode 12.5+
  2. Add https://github.com/Swiftgram/TDLibKit as SPM dependency in Project > Swift Packages. This could take a while cause it downloads ~300mb zip file with binary from TDLibFramework dependency
  3. Add TDLibKit as your target dependency.
  4. Add libz.1.tbd and libc++.1.tbd as your target dependencies.
  5. If something is not accesible from TDLibFramework, make sure to add libSystem.B.tbd for all platforms and libc++abi.tbd if you're building non-macOS app. Source
  6. Code!

Cocoapods

Integration requires similar to TDLibFramework Cocoapods & Flutter guide adaptation.

Usage

Create Client & API instance

import TDLibKit
let client = TdClientImpl(completionQueue: .main)
let api: TdApi = TdApi(client: client)

Synchronious requests

Only for methods with "Can be called synchronously" in docs

let query = SetLogVerbosityLevel(newVerbosityLevel: 5)
let result = api.client.execute(query: DTO(query))

if case .failure(let error) = result {
    print("Error in SetLogVerbosityLevel request \(error.localizedDescription)")
} else if let resultData = try? result.get() {
    print("Response dict \(resultData)")
} else {
    print("Empty Response")
}

Async requests

try? api.getChatHistory(
            chatId: chatId,
            fromMessageId: 0,
            limit: 50,
            offset: 0,
            onlyLocal: false, // Request remote messages from server
            completion: { result in
                // Handle Errors
                if case .failure(let error) = result {
                    self.delegate?.onError(error)
                    print("Error in getChatHistory request \(error.localizedDescription)")
                } else if let messages = try? result.get().messages {
                    // Handle messages
                    for message in messages {
                        switch message.content {
                        case .messageText(let text):
                            print(text.text.text)
                            
                        case .messageAnimation:
                            print("<Animation>")

                        case .messagePhoto(let photo):
                            print("<Photo>\n\(photo.caption.text)")
                            
                        case .messageSticker(let sticker):
                            print(sticker.sticker.emoji)
                            
                        case .messageVideo(let video):
                            print("<Video>\n\(video.caption.text)")
                        
                        // ...

                        default:
                            print("Unknown message content \(message.content)")
                        }
                    }
                }
            }
        )

Listen for updates

api.client.run {
    do {
        let update = try api.decoder.decode(Update.self, from: $0)
        switch update {
            case .updateNewMessage(let newMsg):
                switch newMsg.message.content {
                    case .messageText(let text):
                        print("Text Message: \(text.text.text)")
                    default:
                        break
                }
            case .updateMessageEdited:
                break
                
            // ... etc

            default:
                print("Unhandled Update \(update)")
                break
        }
    } catch {
        print("Error in update handler \(error.localizedDescription)")
    }
}

Logging

You can pass additional parameter with Logger type to log "send, receive, execute" and custom entries.

import TDLibKit
public final class StdOutLogger: Logger {
    
    let queue: DispatchQueue
    
    public init() {
        queue = DispatchQueue(label: "Logger", qos: .userInitiated)
    }
    
    public func log(_ message: String, type: LoggerMessageType?) {
        queue.async {
            var fisrtLine = "---------------------------"
            if let type = type {
                fisrtLine = ">> \(type.description): ---------------"
            }
            print("""
                \(fisrtLine)
                \(message)
                ---------------------------
                """)
        }
    }
}


let client = TdClientImpl(completionQueue: .main, logger: StdOutLogger())

Build

You can find more about build process in Github Actions file.

Credits

License

MIT

GitHub

link
Stars: 1
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

1.0.0-tdlib-1.7.8-bbae7be4
1 week ago

TDLibKit based on TDLib-1.7.8 commit bbae7be4

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics