Skip to content

Commit

Permalink
Merge pull request #15 from peoray/send-message-template
Browse files Browse the repository at this point in the history
  • Loading branch information
peoray authored Nov 15, 2023
2 parents fbc3c21 + 64e98dd commit 5ad0694
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 7 deletions.
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Be sure to keep your API Credentials securely in environment variables.

## Available Services exposed by the SDK

### Sender ID
### Sender ID API

A Sender ID is the name or number that identifies the sender of an SMS message.

Expand Down Expand Up @@ -109,7 +109,7 @@ console.log(response) // IRequestSenderIDResponse

Find more details about the parameters and response for the above method [here](https://developers.termii.com/sender-id#request-sender-id)

### Messaging
### Messaging API

This API allows businesses send text messages to their customers across different messaging channels.

Expand Down Expand Up @@ -158,7 +158,7 @@ console.log(response) // ISendBulkMessageResponse

Find more details about the parameters and response for the above method [here](https://developers.termii.com/messaging-api#send-bulk-message)

### Number
### Number API

This allows businesses send messages to customers using Termii's auto-generated messaging numbers that adapt to customers location.

Expand All @@ -179,6 +179,33 @@ console.log(response) // ISendMessageWithNumberResponse

Find more details about the parameters and response for the above method [here](https://developers.termii.com/number#send-message)

### Templates API

This helps businesses set a template for the one-time-passwords (pins) sent to their customers via whatsapp or sms.

#### Device Template (Send Message with Template)

```ts
// import the template interfaces from the sdk
import type { IDeviceTemplate, IDeviceTemplateResponse } from 'termii-nodejs-client';

const payload: IDeviceTemplate = {
phone_number: '+1234567890',
device_id: 'device123',
template_id: 'template456',
data: {
product_name: 'Dummy Product',
otp: 123456,
expiry_time: '2023-11-16T12:00:00Z',
},
}

const response = await termii.message.sendMessageWithTemplate(payload)
console.log(response) // IDeviceTemplateResponse
```

Find more details about the parameters and response for the above method [here](https://developers.termii.com/number#send-message)

## License

[MIT](https://github.com/peoray/termii-nodejs-client/blob/main/LICENSE)
7 changes: 7 additions & 0 deletions src/services/messaging/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Message } from './message/message'
import { SenderId } from './sender-id'
import { Number } from './number/number'
import { Template } from './template/template'

/**
* The MessageHandler class handles message-related functionalities by providing access to message and sender ID instances.
Expand All @@ -9,6 +10,7 @@ export class MessageHandler {
private messageInstance: Message // Instance to handle sending messages
private senderIdInstance: SenderId // Instance to handle sender IDs
private numberInstance: Number // Instance to handle sending message with auto generated number
private templateInstance: Template // Instance to handle sending message with template

/**
* Constructs a MessageHandler instance.
Expand All @@ -20,6 +22,7 @@ export class MessageHandler {
this.messageInstance = new Message(apiKey)
this.senderIdInstance = new SenderId(apiKey)
this.numberInstance = new Number(apiKey)
this.templateInstance = new Template(apiKey)
}

/**
Expand Down Expand Up @@ -52,6 +55,10 @@ export class MessageHandler {
sendMessageWithNumber: this.numberInstance.sendMessageWithNumber.bind(
this.numberInstance
),
// Method to send a message with template
sendMessageWithTemplate: this.templateInstance.sendMessageWithTemplate.bind(
this.templateInstance
),
}
}
}
7 changes: 3 additions & 4 deletions src/services/messaging/number/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ISendMessageWithNumber,
ISendMessageWithNumberResponse,
} from '../../../types'
import { IAxiosStruct } from '../../../utils'
import { IAxiosStruct, handleErrors } from '../../../utils'

export class Number extends TermiiCore {
constructor(apiKey: string) {
Expand All @@ -17,15 +17,14 @@ export class Number extends TermiiCore {
const requestObj: IAxiosStruct = {
method: 'POST',
url: `/sms/number/send`,
data: data as ISendMessageWithNumber,
data,
}

const response = await this.useRequest(requestObj)

return response?.data as ISendMessageWithNumberResponse
} catch (error) {
// return handleErrors(error)
throw error
return handleErrors(error)
}
}
}
38 changes: 38 additions & 0 deletions src/services/messaging/template/template.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Template } from './template'
import { IDeviceTemplate, IDeviceTemplateResponse } from '../../../types'

const templateInstance = new Template('api-key')

const mockRequest: IDeviceTemplate = {
phone_number: '+1234567890',
device_id: 'device123',
template_id: 'template456',
data: {
product_name: 'Dummy Product',
otp: 123456,
expiry_time: '2023-11-16T12:00:00Z',
},
}

const requestResponse: IDeviceTemplateResponse = {
code: '200',
message_id: '123456789',
message: 'Message sent successfully',
balance: 50.0,
user: 'JohnDoe',
}

describe('Template', () => {
it('should send a message with a template', async () => {
try {
const response = await templateInstance.sendMessageWithTemplate(
mockRequest
)
expect(response).toHaveBeenCalledWith(mockRequest)
expect(response.code).toHaveProperty('200')
expect(response).toEqual(requestResponse)
} catch (error) {
expect(error).toBeInstanceOf(Error)
}
})
})
27 changes: 27 additions & 0 deletions src/services/messaging/template/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TermiiCore } from '../../../api'
import { IDeviceTemplate, IDeviceTemplateResponse } from '../../../types'
import { IAxiosStruct, handleErrors } from '../../../utils'

export class Template extends TermiiCore {
constructor(apiKey: string) {
super(apiKey)
}

public async sendMessageWithTemplate(
data: IDeviceTemplate
): Promise<IDeviceTemplateResponse> {
try {
const requestObj: IAxiosStruct = {
method: 'POST',
url: `/send/template`,
data,
}

const response = await this.useRequest(requestObj)

return response?.data as IDeviceTemplateResponse
} catch (error) {
return handleErrors(error)
}
}
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './message'
export * from './sender-id'
export * from './number'
export * from './template'
16 changes: 16 additions & 0 deletions src/types/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BaseResponse } from './constants'

interface Data {
product_name: string
otp: number
expiry_time: string
}

export interface IDeviceTemplate {
phone_number: string
device_id: string
template_id: string
data: Data
}

export interface IDeviceTemplateResponse extends BaseResponse {}

0 comments on commit 5ad0694

Please sign in to comment.