DirectLine

Swift client library for Microsoft Bot Framework's Direct Line protocol

11
4
Swift

DirectLine

CocoaPods
Carthage Compatible
Platforms
Twitter: @gonzalezreal

DirectLine is a client library for the Microsoft Bot Framework Direct Line protocol.

Loosely based on the official Javascript DirectLine client, it enables communication between your bot and your iOS app using a simple ReactiveX based interface.

IMPORTANT: This library is still work in progress and not at all production-ready.

Examples

To create a DirectLine object, you must provide either the secret for your bot or a temporary token obtained via the Generate Token API:

import DirectLine

let directLine = DirectLine(token: "f1STR0.p3c4D0r") 

The DirectLine object manages token renewal and web socket connection under the hood, so you don’t have to worry about them.

Post activities to the bot:

let myAccount = ChannelAccount(id: "myUserID", name: "Guille")

directLine
  .post(activity: Activity(from: myAccount, text: "What is my current balance?"))
  .subscribe(
    onNext: { print("Posted activity, assigned ID (\($0.id)") },
    onError: { print("Error posting activity \($0)") }
  )

Listen to activities sent and received from the bot:

directLine.activities
  .subscribe(onNext: { activity in
    print("Received activity \(activity)")
  })

You can leverage Rx operators on the incoming activities. For instance, to see only message activities:

directLine.activities
  .filter { $0.type == .message }
  .subscribe(onNext: { activity in
    print("Received message \(activity)")
  })

The activities stream includes those sent to the bot, so a typical pattern is to filter them out using the from property:

directLine.activities
  .filter { $0.type == .message  && $0.from.id == "yourBotHandle" }
  .subscribe(onNext: { activity in
    print("Received message from the bot \(activity)")
  })

Installation

Using CocoaPods

Add pod DirectLine to your Podfile

Using Carthage

Add git "gonzalezreal/DirectLine" to your Cartfile

Using the Swift Package Manager

Add Package(url: "https://github.com/gonzalezreal/DirectLine.git", majorVersion: 1) to your Package.swift file.

Help & Feedback

  • Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
  • Open a PR if you want to make some change to DirectLine.
  • Contact @gonzalezreal on Twitter.