https://github.com/seven-io/loopback
Official LoopBack component for seven
https://github.com/seven-io/loopback
loopback loopback-component
Last synced: 11 months ago
JSON representation
Official LoopBack component for seven
- Host: GitHub
- URL: https://github.com/seven-io/loopback
- Owner: seven-io
- License: mit
- Created: 2022-10-11T14:00:44.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-05T16:21:55.000Z (12 months ago)
- Last Synced: 2025-02-05T17:31:06.659Z (12 months ago)
- Topics: loopback, loopback-component
- Language: TypeScript
- Homepage: https://www.seven.io
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

## loopback-connector-seven
The official [seven](http://www.seven.io/) connector for [LoopBack](https://loopback.io).
Prior to using you will need to [create an API key](https://help.seven.io/en/api-key-access).
This connector supports the following endpoints of
the [seven REST API](https://docs.seven.io/en/rest-api/first-steps):
- [Sending SMS](https://docs.seven.io/en/rest-api/endpoints/sms#send-sms)
- [Making text-to-speech calls](http://docs.seven.io/en/rest-api/endpoints/voice#send-voice-call)
- [Sending RCS messages](https://docs.seven.io/en/rest-api/endpoints/rcs#send-rcs)
## Installation
In your LoopBack project:
### NPM
$ npm i @seven.io/loopback
### Yarn
$ yarn add @seven.io/loopback
## Setup
### Create a data source
```typescript
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'
import {juggler} from '@loopback/repository'
import {SevenConnector} from '@seven.io/loopback'
const config = {
apiKey: process.env.SEVEN_API_KEY,
connector: SevenConnector,
name: 'seven',
}
@lifeCycleObserver('datasource')
export class SevenDataSource extends juggler.DataSource implements LifeCycleObserver {
static dataSourceName = 'seven'
static readonly defaultConfig = config
constructor(
@inject('datasources.config.seven', {optional: true})
dsConfig: object = config,
) {
super('seven', dsConfig)
}
}
```
### Create a model
```typescript
import {Model, model, property} from '@loopback/repository';
@model()
export class SevenMessage extends Model {
@property({
generated: false,
id: true,
required: true,
type: 'string',
})
operation: 'voice' | 'sms';
@property({
required: true,
type: 'string',
})
to: string;
@property({
required: false,
type: 'string',
})
from: string;
@property({
required: true,
type: 'string',
})
text: string;
constructor(data?: Partial) {
super(data);
}
}
export interface SevenRelations {
// describe navigational properties here
}
export type SevenWithRelations = SevenMessage & SevenRelations;
```
### Create a service
```typescript
import {injectable, BindingScope, Provider, inject} from '@loopback/core'
import {GenericService, getService} from '@loopback/service-proxy'
import {SevenDataSource} from '../datasources'
import {SevenMessage} from '../models'
export interface Seven extends GenericService {
send(data: SevenMessage): Promise;
}
@injectable({scope: BindingScope.TRANSIENT})
export class SevenProvider implements Provider {
constructor(
@inject('datasources.seven')
protected dataSource: SevenDataSource = new SevenDataSource(),
) {
}
value(): Promise {
return getService(this.dataSource)
}
}
```
## Usage
### Send SMS
```typescript
import {inject} from '@loopback/core'
import {post} from '@loopback/rest'
import {SevenMessage} from '../models'
import {Seven} from '../services'
export class SmsController {
@post('/send-sms')
async sms(
@inject('services.Seven') sevenProvider: Seven,
): Promise {
const msg = new SevenMessage({
from: 'optional sender ID',
operation: 'sms',
text: 'This is a test SMS from LoopBack via seven',
to: 'phone number(s) for calling separated by comma',
})
try {
const json = await sevenProvider.send(msg)
console.log('json', json)
} catch (e) {
console.error('e', e)
throw e
}
}
}
```
### Make a text-to-speech call
```typescript
import {inject} from '@loopback/core'
import {post} from '@loopback/rest'
import {SevenMessage} from '../models'
import {Seven} from '../services'
export class TextToSpeechController {
@post('/send-voice')
async voice(
@inject('services.Seven') sevenProvider: Seven,
): Promise {
const msg = new SevenMessage({
from: 'optional caller ID',
operation: 'voice',
text: 'This is a test call from LoopBack via seven',
to: 'the number for calling',
})
try {
const json = await sevenProvider.send(msg)
console.log('json', json)
} catch (e) {
console.error('e', e)
throw e
}
}
}
```
### Send a RCS message
```typescript
import {inject} from '@loopback/core'
import {post} from '@loopback/rest'
import {SevenMessage} from '../models'
import {Seven} from '../services'
export class TextToSpeechController {
@post('/send-rcs')
async rcs(
@inject('services.Seven') sevenProvider: Seven,
): Promise {
const msg = new SevenMessage({
from: 'optional caller ID',
operation: 'rcs',
text: 'This is a test call from LoopBack via seven',
to: 'the number for calling',
})
try {
const json = await sevenProvider.send(msg)
console.log('json', json)
} catch (e) {
console.error('e', e)
throw e
}
}
}
```
## Options
### Send SMS
{
from: 'OPTIONAL_SENDER_ID',
operation: 'sms',
text: 'TEXT_MESSAGE',
to: 'TARGET_PHONE_NUMBER(S)'
}
### Make a text-to-speech call
{
from: 'OPTIONAL_CALLER_ID',
operation: 'call',
text: 'TEXT_OR_XML',
to: 'TARGET_PHONE_NUMBER'
}
### Send RCS
{
from: 'OPTIONAL_AGENT_ID',
operation: 'sms',
text: 'RCS_MESSAGE',
to: 'TARGET_PHONE_NUMBER'
}
### Support
Need help? Feel free to [contact us](https://www.seven.io/en/company/contact/).
[](LICENSE)