forked from fiorix/sms-api-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
203 lines (191 loc) · 6.22 KB
/
index.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SMS API">
<meta name="author" content="sms-api-server authors">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script>
angular.module("sms", [])
.controller("rpcController", function($scope) {
$scope.id = 0;
$scope.conn = null;
$scope.backoff = 1e3;
$scope.resp = {id: null, err: null};
$scope.reset = function() {
$scope.sm = {dst:"", text:"", enc:"", register:"final"};
};
$scope.reset();
$scope.msgok = function() {
switch ($scope.sm.enc) {
case "":
case "latin1":
if ($scope.sm.text.length > 140) return false;
break;
case "ucs2":
if ($scope.sm.text.length > 80) return false;
break;
}
return true;
}
$scope.send = function() {
if (!$scope.conn) {
return;
}
$scope.conn.send(JSON.stringify({
id: $scope.id,
method: "SM.Submit",
params: [$scope.sm]
}));
$scope.reset();
$scope.id++;
};
<!-- TODO: query -->
function conn() {
var ws = new WebSocket("ws://" + location.host + "/v1/ws/jsonrpc");
ws.onopen = function(ev) {
$scope.$apply(function() {
$scope.conn = ws;
$scope.backoff = 1e3;
});
};
ws.onclose = function(ev) {
$scope.$apply(function() {
$scope.conn = null;
$scope.backoff = Math.min($scope.backoff * Math.E, 120e3);
setTimeout(function() { conn(); }, $scope.backoff);
});
};
ws.onmessage = function(ev) {
$scope.$apply(function() {
var j = JSON.parse(ev.data);
$scope.resp.id = j.result ? j.result.message_id : null;
$scope.resp.err = j.error;
});
};
}
conn();
})
.controller("eventsController", function($scope) {
$scope.conn = null;
$scope.backoff = 1e3;
$scope.receipts = [];
function conn() {
var ws = new WebSocket("ws://" + location.host + "/v1/ws/jsonrpc/events");
ws.onopen = function(ev) {
$scope.$apply(function() {
$scope.conn = ws;
$scope.backoff = 1e3;
});
};
ws.onclose = function(ev) {
$scope.$apply(function() {
$scope.conn = null;
$scope.backoff = Math.min($scope.backoff * Math.E, 120e3);
setTimeout(function() { conn(); }, $scope.backoff);
});
};
ws.onmessage = function(ev) {
$scope.$apply(function() {
var j = JSON.parse(ev.data);
$scope.conn.send(JSON.stringify({
id: j.id,
result: "",
error: null
}));
$scope.receipts.push(j.params[0]);
if ($scope.receipts.length == 10) {
$scope.receipts.splice(0, 1);
}
});
};
}
conn();
});
</script>
</head>
<body ng-app="sms">
<div class="container">
<h1>SMS API</h1>
<div ng-controller="rpcController">
<div ng-switch="conn">
<p ng-switch-default><span class="label label-success">Connected</span></p>
<p ng-switch-when="null"><span class="label label-danger">Disconnected</span> <i class="fa fa-spin fa-circle-o-notch"></i></p>
</div>
<div class="panel panel-default">
<div class="panel-heading">Send short message</div>
<div class="panel-body">
<form action="#">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="dst">Phone</label>
<input autofocus id="dst" type="text" class="form-control" placeholder="55-11-236-0873" ng-model="sm.dst">
</div>
</div>
<div class="col-sm-6">
<label for="register">Registered delivery</label>
<div class="form-group">
<select id="register" class="form-control" ng-model="sm.register">
<option value="">Not registered</option>
<option value="final">Final delivery</option>
<option value="failure">Failed delivery</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="msg">Message</label>
<span class="pull-right label" ng-class="msgok() ? 'label-primary' : 'label-danger'">{{sm.text.length}} chars</span>
<input id="msg" type="text" class="form-control" placeholder="É nóis Bozo!" ng-model="sm.text">
</div>
</div>
<div class="col-sm-6">
<label for="enc">Message encoding</label>
<div class="form-group">
<select id="enc" class="form-control" ng-model="sm.enc">
<option value="">ASCII</option>
<option value="latin1">LATIN-1</option>
<option value="ucs2">UCS-2</option>
</select>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" ng-disabled="!conn||!msgok()||!sm.text.length||!sm.dst.length" ng-click="send()">Send</button>
<button type="submit" class="btn btn-default" ng-click="reset()">Reset</button>
<hr ng-show="resp.id||resp.err">
<div class="alert alert-success" ng-show="resp.id">
<button type="button" class="close" ng-click="resp.id=null"><span aria-hidden="true">×</span></button>
Done! Message ID: {{resp.id}}
</div>
<div class="alert alert-danger" ng-show="resp.err">
<button type="button" class="close" ng-click="resp.err=null"><span aria-hidden="true">×</span></button>
Failed: {{resp.err}}
</div>
</form>
</div>
</div>
</div>
<div class="panel panel-default" ng-controller="eventsController">
<div class="panel-heading">Delivery receipts <i ng-show="conn" class="fa fa-spin fa-circle-o-notch pull-right"></i></div>
<div class="panel-body">
<div ng-repeat="r in receipts track by $index">
<!-- src/dst are inverted on purpose -->
<p><span ng-show="r.dst">From <span class="label label-default">{{r.dst}}</span></span> To <span class="label label-primary">{{r.src}}</span> {{r.text}}</p>
</div>
</div>
</div>
</div><!-- /.container -->
</body>
</html>