1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+ package org .fineract .messagegateway .sms .providers .impl .telerivet ;
20
+
21
+ import java .io .IOException ;
22
+ import java .util .Collections ;
23
+
24
+ import org .fineract .messagegateway .configuration .HostConfig ;
25
+ import org .fineract .messagegateway .constants .MessageGatewayConstants ;
26
+ import org .fineract .messagegateway .exception .MessageGatewayException ;
27
+ import org .fineract .messagegateway .sms .domain .SMSBridge ;
28
+ import org .fineract .messagegateway .sms .domain .SMSMessage ;
29
+ import org .fineract .messagegateway .sms .providers .SMSProvider ;
30
+ import org .fineract .messagegateway .sms .providers .impl .twilio .TwilioStatus ;
31
+ import org .fineract .messagegateway .sms .util .SmsMessageStatusType ;
32
+ import org .slf4j .Logger ;
33
+ import org .slf4j .LoggerFactory ;
34
+ import org .springframework .beans .factory .annotation .Autowired ;
35
+ import org .springframework .stereotype .Service ;
36
+
37
+ import com .telerivet .*;
38
+
39
+
40
+
41
+ @ Service (value = "telerivet" )
42
+ public class TelerivetMessageProvider extends SMSProvider {
43
+
44
+ private static final Logger logger = LoggerFactory .getLogger (TelerivetMessageProvider .class );
45
+
46
+ private final String callBackUrl ;
47
+
48
+
49
+ @ Autowired
50
+ public TelerivetMessageProvider (final HostConfig hostConfig ) {
51
+ callBackUrl = String .format ("%s://%s:%d/telerivet/report/" , hostConfig .getProtocol (), hostConfig .getHostName (), hostConfig .getPort ());
52
+ logger .info ("Registering call back to Telerivet:" +callBackUrl );
53
+ }
54
+
55
+ @ Override
56
+ public void sendMessage (SMSBridge smsBridgeConfig , SMSMessage message ) throws MessageGatewayException {
57
+ String providerAPIKey = smsBridgeConfig .getConfigValue (MessageGatewayConstants .PROVIDER_API_KEY ) ;
58
+ String providerProjectId = smsBridgeConfig .getConfigValue (MessageGatewayConstants .PROVIDER_PROJECT_ID ) ;
59
+ String statusURL = callBackUrl ;
60
+ String mobileNumber = message .getMobileNumber ();
61
+ String messageToSend = message .getMessage ();
62
+ try {
63
+ TelerivetAPI tr = new TelerivetAPI (providerAPIKey );
64
+ Project project = tr .initProjectById (providerProjectId );
65
+ logger .info ("Sending SMS to " + mobileNumber + " ..." );
66
+ Message sent_msg = project .sendMessage (Util .options (
67
+ "content" , messageToSend ,
68
+ "to_number" ,mobileNumber ,
69
+ "status-url" ,statusURL ));
70
+ message .setExternalId (sent_msg .getId ());
71
+ logger .info ("TelerivetMessageProvider.sendMessage():" +TelerivetStatus .smsStatus (sent_msg .getStatus ()));
72
+ message .setDeliveryStatus (TwilioStatus .smsStatus (sent_msg .getStatus ()).getValue ()) ;
73
+ if (message .getDeliveryStatus ().equals (SmsMessageStatusType .FAILED .getValue ())) {
74
+ message .setDeliveryErrorMessage (sent_msg .getErrorMessage ());
75
+ logger .error ("Sending SMS to :" +message .getMobileNumber ()+" failed with reason " +sent_msg .getErrorMessage ());
76
+ }
77
+ } catch (IOException e ) {
78
+ logger .error ("ApiException while sending message to :" +message .getMobileNumber ()+" with reason " +e .getMessage ());
79
+ message .setDeliveryStatus (SmsMessageStatusType .FAILED .getValue ());
80
+ message .setDeliveryErrorMessage (e .getMessage ());
81
+ }
82
+
83
+ }
84
+ }
0 commit comments