Skip to content

Commit d3315ed

Browse files
committed
add sislog sms provider
1 parent 2a40f05 commit d3315ed

File tree

5 files changed

+276
-0
lines changed

5 files changed

+276
-0
lines changed

config/data-provider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'africastalking' => false,
2020
'httpsms' => false,
2121
'infobip' => false,
22+
'sislog' => false,
2223
],
2324

2425
'authenticable-providers' => [

config/features.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'africastalking' => false,
3434
'httpsms' => true,
3535
'infobip' => true,
36+
'sislog' => false,
3637
],
3738

3839
// Client limits

src/Ushahidi/DataSource/DataSourceManager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class DataSourceManager
3939
'smssync' => SMSSync\SMSSync::class,
4040
'twilio' => Twilio\Twilio::class,
4141
'twitter' => Twitter\Twitter::class,
42+
'sislog' => Sislog\Sislog::class,
4243
];
4344

4445
/**
@@ -334,4 +335,9 @@ function ($consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_to
334335
}
335336
);
336337
}
338+
339+
protected function createSislogSource(array $config)
340+
{
341+
return new SislogSource($config);
342+
}
337343
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Ushahidi\DataSource\Sislog;
4+
5+
/**
6+
* Sislog Data Provider
7+
*
8+
* @author Ushahidi Team <team@ushahidi.com>
9+
* @package DataSource\Sislog
10+
* @copyright 2024 Ushahidi
11+
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3)
12+
*/
13+
14+
use Illuminate\Routing\Router;
15+
use Illuminate\Support\Facades\Log;
16+
use Ushahidi\DataSource\Contracts\MessageType;
17+
use Ushahidi\DataSource\Contracts\MessageStatus;
18+
use Ushahidi\DataSource\Contracts\CallbackDataSource;
19+
use Ushahidi\DataSource\Contracts\OutgoingDataSource;
20+
use Ushahidi\DataSource\Concerns\MapsInboundFields;
21+
22+
class Sislog implements CallbackDataSource, OutgoingDataSource
23+
{
24+
use MapsInboundFields;
25+
26+
protected $config;
27+
28+
/**
29+
* Client to talk to the sislog API
30+
*
31+
* @var \Vonage\Message\Client
32+
*/
33+
private $client;
34+
35+
protected $defaultServer = '';
36+
37+
/**
38+
* Constructor function for DataSource
39+
*/
40+
public function __construct(array $config, \GuzzleHttp\Client $client = null)
41+
{
42+
$this->config = $config;
43+
$this->client = $client;
44+
}
45+
46+
public function getName()
47+
{
48+
return 'Sislog';
49+
}
50+
51+
public function getId()
52+
{
53+
return strtolower($this->getName());
54+
}
55+
56+
public function getServices()
57+
{
58+
return [MessageType::SMS];
59+
}
60+
61+
public function getOptions()
62+
{
63+
return [
64+
'server_url' => [
65+
'label' => 'FrontlineSMS Server URL',
66+
'input' => 'text',
67+
'description' => 'The URL where the Sislog server is installed, i.e. https://server.url.com/',
68+
'rules' => ['required']
69+
],
70+
'from' => [
71+
'label' => 'From',
72+
'input' => 'text',
73+
'description' => 'The from number',
74+
'rules' => ['required']
75+
],
76+
'api_username' => [
77+
'label' => 'Username',
78+
'input' => 'text',
79+
'description' => 'The API username',
80+
'rules' => ['required']
81+
],
82+
'api_password' => [
83+
'label' => 'Password',
84+
'input' => 'text',
85+
'description' => 'The API password',
86+
'rules' => ['required']
87+
],
88+
// 'api_secret' => [
89+
// 'label' => 'API secret',
90+
// 'input' => 'text',
91+
// 'description' => 'The API secret',
92+
// 'rules' => ['required']
93+
// ]
94+
];
95+
}
96+
97+
public function getInboundFields()
98+
{
99+
return [
100+
'Message' => 'text'
101+
];
102+
}
103+
104+
public function isUserConfigurable()
105+
{
106+
return true;
107+
}
108+
109+
/**
110+
* @return mixed
111+
*/
112+
public function send($to, $message, $title = "", $contact_type = null)
113+
{
114+
// Obtain server url, ensure it ends with '/'
115+
$serverUrl = $this->config['server_url'] ?? $this->defaultServer;
116+
if (substr($serverUrl, -1) != '/') {
117+
$serverUrl = $serverUrl . '/';
118+
}
119+
// Check we have the required config
120+
if (!isset($this->config['api_username']) || !isset($this->config['api_password'])) {
121+
Log::warning('Could not send message with Sislog, incomplete config');
122+
return [MessageStatus::FAILED, false];
123+
}
124+
125+
$auth = 'Authorization: Basic ' . base64_encode("$this->config['api_username']:$this->config['api_password']");
126+
127+
128+
// Prepare data to send to frontline cloud
129+
$data = [
130+
"From" => "",
131+
"To" => $to,
132+
"Content" => $message
133+
// "ClientReference" => $message
134+
];
135+
136+
// Make a POST request to send the data to frontline cloud
137+
138+
try {
139+
$response = $this->client->request('POST', $serverUrl . $this->apiUrl, [
140+
'headers' => [
141+
'Accept' => 'application/json',
142+
'Content-Type' => 'application/json',
143+
'Authorization' => $auth
144+
],
145+
'json' => $data
146+
]);
147+
// Successfully executed the request
148+
149+
if ($response->getStatusCode() === 200) {
150+
return [MessageStatus::SENT, false];
151+
}
152+
153+
// Log warning to log file.
154+
$status = $response->getStatusCode();
155+
Log::warning(
156+
'Could not make a successful POST request',
157+
['message' => $response->messages[$status], 'status' => $status]
158+
);
159+
} catch (\GuzzleHttp\Exception\ClientException | \GuzzleHttp\Exception\RequestException $e) {
160+
// Log warning to log file.
161+
Log::warning(
162+
'Could not make a successful POST request',
163+
['message' => $e->getMessage()]
164+
);
165+
}
166+
}
167+
168+
public static function registerRoutes(Router $router)
169+
{
170+
$router->post('sms/sislog', 'Ushahidi\DataSource\Sislog\SislogController@handleRequest');
171+
}
172+
173+
public function verifySecret($secret)
174+
{
175+
if (isset($this->config['secret']) and $secret === $this->config['secret']) {
176+
return true;
177+
}
178+
179+
return false;
180+
}
181+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Ushahidi\DataSource\Sislog;
4+
5+
/**
6+
* Base class for all Data Providers
7+
*
8+
* @author Ushahidi Team <team@ushahidi.com>
9+
* @package Ushahidi\DataSource
10+
* @copyright 2024 Ushahidi
11+
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
12+
*/
13+
14+
use Ushahidi\DataSource\DataSourceController;
15+
use Ushahidi\Contracts\Contact;
16+
use Ushahidi\DataSource\Contracts\MessageType;
17+
use Illuminate\Http\Request;
18+
19+
class SislogController extends DataSourceController
20+
{
21+
protected $source = 'sislog';
22+
23+
public function handleRequest(Request $request)
24+
{
25+
//// Authenticate the request
26+
// if (!$this->source->verifySecret($request->input('secret'))) {
27+
// return response(['payload' => [
28+
// 'success' => false,
29+
// 'error' => 'Incorrect or missing secret key'
30+
// ]], 403);
31+
// }
32+
33+
// Process incoming messages from Sislog only if the request is POST
34+
if ($request->method() == 'POST') {
35+
return $this->incoming($request);
36+
}
37+
38+
39+
// Set the response
40+
return ['payload' => [
41+
'success' => true,
42+
'error' => null
43+
]];
44+
}
45+
46+
/**
47+
* Process messages received from Sislog
48+
*/
49+
private function incoming($request)
50+
{
51+
$from = $request->input('msisdn');
52+
53+
if (empty($from)) {
54+
return response(['payload' => [
55+
'success' => false,
56+
'error' => 'Missing from value'
57+
]], 400);
58+
}
59+
60+
$message_text = $request->input('msg');
61+
62+
if (empty($message_text)) {
63+
return response(['payload' => [
64+
'success' => false,
65+
'error' => 'Missing message'
66+
]], 400);
67+
}
68+
69+
// Allow for Alphanumeric sender
70+
$from = preg_replace("/[^0-9A-Za-z+ ]/", "", $from);
71+
72+
73+
$this->save([
74+
'type' => MessageType::SMS,
75+
'from' => $from,
76+
'contact_type' => Contact::PHONE,
77+
'message' => $message_text,
78+
'title' => null,
79+
'data_source' => 'sislog'
80+
]);
81+
82+
return ['payload' => [
83+
'success' => true,
84+
'error' => null
85+
]];
86+
}
87+
}

0 commit comments

Comments
 (0)