Swiftpack.co - vapor-community/VaporTwilioService as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vapor-community.
vapor-community/VaporTwilioService 4.0.0
Twilio API provider for all your Vapor needs
⭐️ 19
🕓 52 weeks ago
macOS linux macOS iOS
.package(url: "https://github.com/vapor-community/VaporTwilioService.git", from: "4.0.0")
Documentation Team Chat MIT License Continuous Integration Github Actions Swift 5.1

What

This is a wrapper service for interacting with the Twilio API for Vapor4

Note: Vapor3 version is available in vapor3 branch and from 1.0.0 tag

Installation

// dependencies
.package(url: "https://github.com/twof/VaporTwilioService.git", from: "2.0.0")

// Targets
.target(name: "App", dependencies: [
    .product(name: "Vapor", package: "vapor"),
    .product(name: "Twilio", package: "VaporTwilioService")
])

Usage

Setup

import Twilio

// Called before your application initializes.
func configure(_ app: Application) throws {
    /// case 1
    /// put into your environment variables the following keys:
    /// TWILIO_ACCOUNT_ID=...
    /// TWILIO_ACCOUNT_SECRET=...
    app.twilio.configuration = .environment

    /// case 2
    /// manually
    app.twilio.configuration = .init(accountId: "<account id>", accountSecret: "<account secret>")
}

Sending a text

In route handler

import Twilio

func routes(_ app: Application) throws {
    app.get { req -> EventLoopFuture<ClientResponse> in
        let sms = OutgoingSMS(body: "Hey There", from: "+18316100806", to: "+14083688346")
        return req.twilio.send(sms)
    }
}

Anywhere else

import Twilio

public func configure(_ app: Application) throws {
    // ...
    // e.g. in the very end
    let sms = OutgoingSMS(body: "Hey There", from: "+18316100806", to: "+14083688346")
    app.twilio.send(sms).whenSuccess { response in
        print("just sent: \(response)")
    }
}

Handling Incoming Texts

After setting up the necessary routing within your Twilio account, you may create routes to handle and respond to incoming texts.

import Twilio

func routes(_ app: Application) throws {
    app.post("incoming") { req -> Response in
        let sms = try req.content.decode(IncomingSMS.self)

        let responseMessage = SMSResponse(
            Message(body: "Hello Friend!"),
            Message(body: "This is a second text.")
        )

        return req.twilio.respond(with: responseMessage)
    }
}

GitHub

link
Stars: 19
Last commit: 52 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

Support for Vapor 4
4 years ago

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