Skip to content

Commit

Permalink
Move config file, add methods to override API key and Project ID
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbjr committed Aug 30, 2020
1 parent 78837e3 commit 7ee9eea
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 28 deletions.
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ This package makes it easy to send notifications using [Telerivet](https://teler
- [Credits](#credits)
- [License](#license)

## Upgrading from v1.x to v2.x

In v2.x, we've moved the configuration settings of Telerivet from `config/broadcasting.php` to `config/telerivet.php`.

To migrate to v2.x, simply run the following command to get the configuration file:

```
php artisan vendor:publish --provider="CoreProc\NotificationChannels\Telerivet\TelerivetServiceProvider"
```

## Installation

Expand All @@ -37,17 +46,17 @@ Register the ServiceProvider in your config/app.php (Skip this step if you are u
You need to register for an API key and a number for outgoing SMS here:
[https://telerivet.com](https://telerivet.com)

Once you've registered and set up your project and numbers, add the API key and project ID to your configuration in
`config/broadcasting.php`

'connections' => [
....
'telerivet' => [
'api_key' => env('TELERIVET_API_KEY'),
'project_id' => env('TELERIVET_PROJECT_ID'),
],
...
]
Once you've registered and set up your project and numbers, get the configuration file by running the following command:

```
php artisan vendor:publish --provider="CoreProc\NotificationChannels\Telerivet\TelerivetServiceProvider"
```

Add the API key and project ID to your configuration in `config/telerivet.php`. You can set the credentials in your
`.env` file with the following variables: `TELERIVET_API_KEY` and `TELERIVET_PROJECT_ID`.

Optionally, you can also override these configurations by calling the `setApiKey()` and `setProjectId()` methods
in your `TelerivetMessage` object.

## Usage

Expand Down
11 changes: 11 additions & 0 deletions config/telerivet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [

'api_key' => env('TELERIVET_API_KEY'),

'project_id' => env('TELERIVET_PROJECT_ID'),

'url' => env('TELERIVET_URL', 'https://api.telerivet.com'),

];
22 changes: 20 additions & 2 deletions src/TelerivetChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function send($notifiable, Notification $notification)
try {
/** @var Response $response */
$response = $this->client->post(
'v1/projects/' . config('broadcasting.connections.telerivet.project_id') . '/messages/send',
'v1/projects/' . $this->getProjectId($telerivetMessage) . '/messages/send',
[
'auth' => [config('broadcasting.connections.telerivet.api_key'), ''],
'auth' => [$this->getApiKey($telerivetMessage), ''],
'json' => $telerivetMessage->toArray(),
]
);
Expand All @@ -69,4 +69,22 @@ public function send($notifiable, Notification $notification)

return $response;
}

protected function getProjectId(TelerivetMessage $telerivetMessage)
{
if (! empty($telerivetMessage->getProjectId())) {
return $telerivetMessage->getProjectId();
}

return config('telerivet.project_id');
}

protected function getApiKey(TelerivetMessage $telerivetMessage)
{
if (! empty($telerivetMessage->getApiKey())) {
return $telerivetMessage->getApiKey();
}

return config('telerivet.api_key');
}
}
82 changes: 67 additions & 15 deletions src/TelerivetMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@

class TelerivetMessage
{
/**
* Optionally override the project ID from the config
*
* @var string|null
*/
protected $projectId;

/**
* Optionally override the API key from the config
*
* @var string|null
*/
protected $apiKey;

/**
* @var string
*/
Expand Down Expand Up @@ -94,6 +108,44 @@ class TelerivetMessage
*/
protected $ttsVoice;

/**
* @return string|null
*/
public function getProjectId(): ?string
{
return $this->projectId;
}

/**
* @param string|null $projectId
* @return TelerivetMessage
*/
public function setProjectId(?string $projectId): self
{
$this->projectId = $projectId;

return $this;
}

/**
* @return string|null
*/
public function getApiKey(): ?string
{
return $this->apiKey;
}

/**
* @param string|null $apiKey
* @return TelerivetMessage
*/
public function setApiKey(?string $apiKey): self
{
$this->apiKey = $apiKey;

return $this;
}

/**
* @return string
*/
Expand All @@ -107,7 +159,7 @@ public function getMessageType(): ?string
* Possible Values: sms, mms, ussd, call, text
* Default: text
*
* @param string $messageType
* @param string|null $messageType
* @return TelerivetMessage
*/
public function setMessageType(?string $messageType): self
Expand All @@ -129,7 +181,7 @@ public function getContent(): ?string
* [Required if sending SMS message]
* Content of the message to send (if message_type is call, the text will be spoken during a text-to-speech call)
*
* @param string $content
* @param string|null $content
* @return TelerivetMessage
*/
public function setContent(?string $content): self
Expand All @@ -151,7 +203,7 @@ public function getToNumber(): ?string
* [Required if contact_id not set]
* Phone number to send the message to.
*
* @param string $toNumber
* @param string|null $toNumber
* @return TelerivetMessage
*/
public function setToNumber(?string $toNumber): self
Expand All @@ -173,7 +225,7 @@ public function getContactId(): ?string
* [Required if to_number not set]
* ID of the contact to send the message to
*
* @param string $contactId
* @param string|null $contactId
* @return TelerivetMessage
*/
public function setContactId(?string $contactId): self
Expand All @@ -195,7 +247,7 @@ public function getRouteId(): ?string
* [Optional] ID of the phone or route to send the message from
* Default: default sender route ID for your project
*
* @param string $routeId
* @param string|null $routeId
* @return TelerivetMessage
*/
public function setRouteId(?string $routeId): self
Expand All @@ -216,7 +268,7 @@ public function getStatusUrl(): ?string
/**
* [Optional] Webhook callback URL to be notified when message status changes
*
* @param string $statusUrl
* @param string|null $statusUrl
* @return TelerivetMessage
*/
public function setStatusUrl(?string $statusUrl): self
Expand All @@ -237,7 +289,7 @@ public function getStatusSecret(): ?string
/**
* [Optional] POST parameter 'secret' passed to status_url
*
* @param string $statusSecret
* @param string|null $statusSecret
* @return TelerivetMessage
*/
public function setStatusSecret(?string $statusSecret): self
Expand Down Expand Up @@ -304,7 +356,7 @@ public function getMediaUrls(): ?array
* [Optional] URLs of media files to attach to the text message. If message_type is sms, short links to each media
* URL will be appended to the end of the content (separated by a new line).
*
* @param array $mediaUrls
* @param array|null $mediaUrls
* @return TelerivetMessage
*/
public function setMediaUrls(?array $mediaUrls): self
Expand All @@ -326,7 +378,7 @@ public function getLabelIds(): ?array
* [Optional] Array string IDs of Label <https://telerivet.com/api/rest/curl#Label>
* List of IDs of labels to add to this message
*
* @param array $labelIds
* @param array|null $labelIds
* @return TelerivetMessage
*/
public function setLabelIds(?array $labelIds): self
Expand All @@ -347,7 +399,7 @@ public function getVars(): ?object
/**
* [Optional] Custom variables to store with the message
*
* @param object $vars
* @param object|null $vars
* @return TelerivetMessage
*/
public function setVars(?object $vars): self
Expand All @@ -371,7 +423,7 @@ public function getPriority(): ?int
* Possible Values: 1, 2
* Default: 1
*
* @param int $priority
* @param int|null $priority
* @return TelerivetMessage
*/
public function setPriority(?int $priority): self
Expand Down Expand Up @@ -415,7 +467,7 @@ public function getServiceId(): ?string
* [Optional] string ID of Service <https://telerivet.com/api/rest/curl#Service>
* Service that defines the call flow of the voice call (when message_type is call)
*
* @param string $serviceId
* @param string|null $serviceId
* @return TelerivetMessage
*/
public function setServiceId(?string $serviceId): self
Expand All @@ -440,7 +492,7 @@ public function getAudioUrl(): ?string
* For best results, use an MP3 file containing only speech. Music is not recommended because the audio quality will
* be low when played over a phone line.
*
* @param string $audioUrl
* @param string|null $audioUrl
* @return TelerivetMessage
*/
public function setAudioUrl(?string $audioUrl): self
Expand All @@ -464,7 +516,7 @@ public function getTtsLang(): ?string
* pt-BR, pt-PT, ru-RU, es-ES, es-US, sv-SE
* Default: en-US
*
* @param string $ttsLang
* @param string|null $ttsLang
* @return TelerivetMessage
*/
public function setTtsLang(?string $ttsLang): self
Expand All @@ -487,7 +539,7 @@ public function getTtsVoice(): ?string
* Possible Values: female, male
* Default: female
*
* @param string $ttsVoice
* @param string|null $ttsVoice
* @return TelerivetMessage
*/
public function setTtsVoice(?string $ttsVoice): self
Expand Down
4 changes: 4 additions & 0 deletions src/TelerivetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ public function boot()
'base_uri' => config('broadcasting.connections.telerivet.url', TelerivetChannel::DEFAULT_API_URL),
]);
});

$this->publishes([
__DIR__ . '/../config/telerivet.php' => config_path('telerivet.php'),
], 'telerivet-config');
}
}

0 comments on commit 7ee9eea

Please sign in to comment.