-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtwilio.service.dart
91 lines (75 loc) · 2.38 KB
/
twilio.service.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import 'dart:convert';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
import 'dart:math' as math;
import 'package:safe/utils/text/text.util.dart';
class TwilioService {
static const String endpoint = "https://api.twilio.com";
static const String version = "2010-04-01";
Uri _generateUri({required String sid, required String type}) {
return Uri.parse(
"$endpoint/$version/Accounts/$sid/$type.json",
);
}
String _selectPhoneNumber(
List<Map<String, int>> tally,
void Function(List<Map<String, int>>) setTally,
) {
Map<String, int> champ = tally[math.Random().nextInt(tally.length)];
for (final entry in tally) {
if (entry.values.first < champ.values.first) {
champ = entry;
}
}
tally.remove(champ);
champ[champ.keys.first] = champ.values.first + 1;
tally.add(champ);
setTally(tally);
return champ.keys.first;
}
Future<Map?> call({
required String phone,
required String message,
required List<Map<String, int>> tally,
required void Function(List<Map<String, int>>) setTally,
}) async {
final body = {
"To": phone,
"From": dotenv.env[_selectPhoneNumber(tally, setTally)],
"Twiml": "<Response><Say>$message</Say></Response>",
};
final response = await _request(body, "Calls");
return jsonDecode(response.body);
}
Future<Map<String, dynamic>?> messageSMS({
required String phone,
required String message,
required List<Map<String, int>> tally,
required void Function(List<Map<String, int>>) setTally,
}) async {
final body = {
"To": phone,
"From": dotenv.env[_selectPhoneNumber(tally, setTally)],
"Body": TextUtil.removeNonUSCChars(message),
};
final response = await _request(body, "Messages");
return jsonDecode(response.body);
}
Future<http.Response> _request(Map body, String type) async {
// Generates headers in propper format
final bytes = utf8.encode(
"${dotenv.env["TWILIO_SID"]}:${dotenv.env["TWILIO_TOKEN"]}",
);
final base64String = base64.encode(bytes);
// Generates header values in map
final headers = {
"Authorization": "Basic $base64String",
"Accept": "application/json"
};
return http.post(
_generateUri(sid: dotenv.env["TWILIO_SID"]!, type: type),
headers: headers,
body: body,
);
}
}