https://github.com/vapor-community/vaportwilioservice
Twilio API provider for all your Vapor needs
https://github.com/vapor-community/vaportwilioservice
api sdk server-side-swift swift swift-4 twilio vapor
Last synced: 8 months ago
JSON representation
Twilio API provider for all your Vapor needs
- Host: GitHub
- URL: https://github.com/vapor-community/vaportwilioservice
- Owner: vapor-community
- Created: 2019-01-09T02:47:24.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-26T02:16:55.000Z (over 2 years ago)
- Last Synced: 2025-04-04T22:46:49.548Z (8 months ago)
- Topics: api, sdk, server-side-swift, swift, swift-4, twilio, vapor
- Language: Swift
- Homepage:
- Size: 38.1 KB
- Stars: 22
- Watchers: 3
- Forks: 12
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
```swift
// 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
```swift
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: "", accountSecret: "")
}
```
### Sending a text
#### In route handler
```swift
import Twilio
func routes(_ app: Application) throws {
app.get { req -> EventLoopFuture in
let sms = OutgoingSMS(body: "Hey There", from: "+18316100806", to: "+14083688346")
return req.twilio.send(sms)
}
}
```
#### Anywhere else
```swift
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](https://www.twilio.com/docs/sms/twiml#twilios-request-to-your-application), you may create routes to handle and respond to incoming texts.
```swift
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)
}
}
```