Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace smsgateway_modica;
18
 
19
use core\http_client;
20
use core_sms\manager;
21
use core_sms\message;
22
use GuzzleHttp\Exception\GuzzleException;
23
 
24
/**
25
 * Modica SMS gateway.
26
 *
27
 * @see https://confluence.modicagroup.com/display/DC/Mobile+Gateway+REST+API#MobileGatewayRESTAPI-Sendingtoasingledestination
28
 * @package    smsgateway_modica
29
 * @copyright  2025 Safat Shahin <safat.shahin@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class gateway extends \core_sms\gateway {
33
    /**
34
     * @var string MODICA_DEFAULT_API The default api gateway for modica.
35
     */
36
    public const MODICA_DEFAULT_API = 'https://api.modicagroup.com/rest/gateway/messages';
37
 
38
    #[\Override]
39
    public function send(
40
        message $message,
41
    ): message {
42
        $recipientnumber = manager::format_number(
43
            phonenumber: $message->recipientnumber,
44
            countrycode: $this->config->countrycode ?? null,
45
        );
46
        $json = json_encode(
47
            [
48
                'destination' => $recipientnumber,
49
                'content' => $message->content,
50
            ],
51
            JSON_THROW_ON_ERROR,
52
        );
53
 
54
        $client = \core\di::get(http_client::class);
55
        try {
56
            $response = $client->post(
57
                uri: $config->modica_url ?? self::MODICA_DEFAULT_API,
58
                options: [
59
                    'auth' => [
60
                        $this->config->modica_application_name,
61
                        $this->config->modica_application_password,
62
                    ],
63
                    'headers' => ['Content-Type' => 'application/json'],
64
                    'body' => $json,
65
                ],
66
            );
67
 
68
            if ($response->getStatusCode() === 201) {
69
                $status = \core_sms\message_status::GATEWAY_SENT;
70
            } else {
71
                $status = \core_sms\message_status::GATEWAY_FAILED;
72
            }
73
        } catch (GuzzleException $e) {
74
            $status = \core_sms\message_status::GATEWAY_FAILED;
75
        }
76
 
77
        return $message->with(
78
            status: $status,
79
        );
80
    }
81
 
82
    #[\Override]
83
    public function get_send_priority(message $message): int {
84
        return 50;
85
    }
86
}