ola_api.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. if ("MozWebSocket" in window) {
  2. WebSocket = MozWebSocket;
  3. }
  4. var ola = {
  5. version : '2.0.1',
  6. ws: undefined,
  7. uuid: "73836387-0000-0000-0000-0000-0000000000",
  8. connected: false,
  9. socket_connected: false,
  10. onConnect: undefined,
  11. onMessage: undefined,
  12. onClose: undefined,
  13. _connectSuccess: undefined,
  14. _connectError: undefined,
  15. _username: undefined,
  16. _password: undefined,
  17. _extn: undefined,
  18. connect: function(url, username, password, success, error) {
  19. if (!url) url = "ws://" + document.location.hostname + ':' + document.location.port + "/ola_socket";
  20. if ("WebSocket" in window) {
  21. // browser supports websockets
  22. this.ws = new WebSocket(url);
  23. if (this.ws) {
  24. this.ws.onclose = ola.onClose;
  25. this.ws.onopen = ola._onOpen;
  26. this.ws.onmessage = ola._onMessage;
  27. this._connectSuccess = success;
  28. this._connectError = error;
  29. this._username = username;
  30. this._password = password;
  31. }
  32. } else {
  33. // browser does not support websockets
  34. if ("console" in window) {
  35. console.log("you don't have websocket");
  36. }
  37. return false;
  38. }
  39. return this;
  40. },
  41. close: function() {
  42. ola.ws.close();
  43. },
  44. _onOpen: function() {
  45. ola.auth(ola._username, ola._password);
  46. },
  47. _onMessage: function (evt) {
  48. console.log(evt.data);
  49. var msg = JSON.parse(evt.data);
  50. if (msg.event_name == "command/reply" && msg.code == 200) {
  51. ola.ws.onmessage = this.onMessage;
  52. }
  53. ola.ws.onmessage = ola.onMessage;
  54. ola.onConnect();
  55. },
  56. auth: function(username, password) {
  57. return ola.send({cmd: "auth", args:{username:username, password:password, accept:"application/json"}});
  58. },
  59. get_agent_state: function() {
  60. return this.send({action:"api", cmd:"get_agent_state", args:{extn: this._extn}});
  61. },
  62. get_trunk_state: function() {
  63. return this.send({action:"api", cmd:"get_trunk_state"});
  64. },
  65. login : function(queue, extn, params) {
  66. var args = {queue:queue, extn: extn};
  67. this.merge(args, params);
  68. this._extn = extn;
  69. return ola.send({action:"api", cmd:"login", args: args });
  70. },
  71. logout: function() {
  72. return this.send({action:"api", cmd:"logout", args:{extn: this._extn}});
  73. },
  74. ping: function() {
  75. return ola.send({cmd: "ping"})
  76. },
  77. subscribe: function(k) {
  78. return ola.send({cmd:"subscribe", args:{key:k}});
  79. },
  80. unsubscribe: function(k) {
  81. return ola.send({cmd:"unsubscribe", args:{key:k}});
  82. },
  83. go_ready: function() {
  84. return this.send({action:"api", cmd:"go_ready", args:{extn: this._extn}});
  85. },
  86. go_ready2: function(extn) {
  87. return this.send({action:"api", cmd:"go_ready", args:{extn: extn}});
  88. },
  89. go_break: function(reason) {
  90. return this.send({action:"api", cmd:"go_break", args:{extn: this._extn, reason: reason}});
  91. },
  92. go_break2: function(extn) {
  93. return this.send({action:"api", cmd:"go_break", args:{extn: extn, reason: ""}});
  94. },
  95. toggle_ready : function() {
  96. return this.send({action:"api", cmd:"toggle_ready", args:{extn: this._extn}});
  97. },
  98. answer : function() {
  99. return this.send({action:"api", cmd:"answer", args:{extn: this._extn}});
  100. },
  101. hangup : function() {
  102. return this.send({action:"api", cmd:"hangup_other", args:{extn: this._extn}});
  103. },
  104. dial : function(dst, gateway) {
  105. return this.send({action:"api", cmd:"dial", args:{extn: this._extn, dest: dst, gateway: gateway}});
  106. },
  107. transfer : function(dst, src) {
  108. var extn = src;
  109. if (extn == null || typeof(extn) == "undefined") {
  110. extn = this._extn;
  111. }
  112. return this.send({action:"api", cmd:"transfer", args:{extn:extn, dest:dst}});
  113. },
  114. transfer_uuid : function(uuid, dst) {
  115. return this.send({action:"api", cmd:"transfer", args:{channel_uuid:uuid, dest:dst}});
  116. },
  117. monitor : function(dst, src) {
  118. var extn = src;
  119. if (extn == null || typeof(extn) == "undefined") {
  120. extn = this._extn;
  121. }
  122. return this.send({action:"api", cmd:"monitor", args:{extn: extn, dest: dst}});
  123. },
  124. monitor_uuid: function(uuid) {
  125. return this.send({action:"api", cmd:"monitor", args:{extn: this._extn, channel_uuid: uuid}});
  126. },
  127. intercept: function(dst, src) {
  128. var extn = src;
  129. if (extn == null || typeof(extn) == "undefined") {
  130. extn = this._extn;
  131. }
  132. return this.send({action:"api", cmd:"intercept", args:{extn: extn, dest: dst}});
  133. },
  134. intercept_uuid: function(uuid) {
  135. return this.send({action:"api", cmd:"intercept", args:{extn: this._extn, channel_uuid: uuid}});
  136. },
  137. three_way : function(dst, src) {
  138. var extn = src;
  139. if (extn == null || typeof(extn) == "undefined") {
  140. extn = this._extn;
  141. }
  142. return this.send({action:"api", cmd:"monitor", args:{extn: extn, dest: dst, three_way:"true"}});
  143. },
  144. three_way_uuid : function(uuid) {
  145. return this.send({action:"api", cmd:"monitor", args:{extn: this._extn, channel_uuid: uuid, three_way:"true"}});
  146. },
  147. unmonitor : function(ext) {
  148. return this.send({action:"api", cmd:"unmonitor", args:{extn: ext}});
  149. },
  150. whisper : function(w) {
  151. // who = "agent" / "caller" / "both" / "none"
  152. return this.send({action:"api", cmd:"whisper", args:{extn: this._extn, who: w}});
  153. },
  154. consult : function(dst) {
  155. return this.send({action:"api", cmd:"consult", args:{extn: this._extn, dest: dst}});
  156. },
  157. hold : function() {
  158. return this.send({action:"api", cmd:"hold", args:{extn: this._extn}});
  159. },
  160. unhold : function(dst) {
  161. return this.send({action:"api", cmd:"unhold", args:{extn: this._extn}});
  162. },
  163. toggle_hold : function() {
  164. return this.send({action:"api", cmd:"toggle_hold", args:{extn: this._extn}});
  165. },
  166. take_call : function(channel_uuid) {
  167. return this.send({action:"api", cmd:"take_call", args:{extn: this._extn, channel_uuid:channel_uuid}});
  168. },
  169. conference : function(dst) {
  170. return this.send({action:"api", cmd:"conference", args:{extn: this._extn, dest: dst}});
  171. },
  172. conference_uuid : function(uuid) {
  173. return this.send({action:"api", cmd:"conference", args:{extn: this._extn, channel_uuid: uuid}});
  174. },
  175. broadcast : function(numbers, mute) {
  176. return this.send({action:"api", cmd:"broadcast", args:{extn: this._extn, numbers: numbers, mute:mute}});
  177. },
  178. /* send chat to a queue or an agent
  179. to = queue send to queue
  180. to = queue.agent send to agent
  181. */
  182. chat: function (to, message, content_type) {
  183. return this.send({action:"api", cmd:"chat", args:{to:to, message:message, content_type:content_type}});
  184. },
  185. message: function (from, to, message, content_type) {
  186. return this.send({action:"api", cmd:"message", args:{from:from, to:to, message:message, content_type:content_type}});
  187. },
  188. alarm: function (queue, state) {
  189. return this.send({action:"api", cmd:"alarm", args:{queue:queue, state:state}});
  190. },
  191. /* dispatching apis */
  192. dlogin: function (ext) {
  193. return this.send({action:"api", cmd:"dlogin", args:{extn: ext}});
  194. },
  195. dlogout: function (ext) {
  196. return this.send({action:"api", cmd:"dlogout", args:{extn: ext}});
  197. },
  198. inject: function (ext, uuid) {
  199. return this.send({action:"api", cmd:"inject", args:{extn: ext, channel_uuid: uuid}});
  200. },
  201. kill: function(uuid, cause) {
  202. return this.send({action:"api", cmd:"kill", args:{channel_uuid: uuid, cause: cause}});
  203. },
  204. eavesdrop: function(uuid) {
  205. return this.send({action:"api", cmd:"eavesdrop", args:{channel_uuid: uuid}});
  206. },
  207. conf: function(name, action, member) {
  208. return this.send({action:"api", cmd:"conf", args:{name: name, action:action, member:member}});
  209. },
  210. answer_all: function(ext, queue) {
  211. return this.send({action:"api", cmd:"answer_all", args:{extn: ext, queue:queue}});
  212. },
  213. group_call: function(ext, queue, numbers, batch_accept) {
  214. return this.send({action:"api", cmd:"group_call", args:{extn: ext, queue:queue, numbers:numbers, batch_accept:batch_accept}});
  215. },
  216. sip_gateway: function (profile, gateway, op) {
  217. return this.send({action:"api", cmd:"sip_gateway", args:{profile:profile, gateway:gateway, op:op}});
  218. },
  219. play: function (conference, filename) {
  220. return this.send({action:"api", cmd:"play", args:{conference:conference, filename:filename}});
  221. },
  222. stop_play: function (conference, filename) {
  223. return this.send({action:"api", cmd:"play", args:{conference:conference, filename:filename}});
  224. },
  225. merge_call: function(ext1, ext2) {
  226. return this.send({action:"api", cmd:"merge_call", args:{extn1:ext1, extn2:ext2}});
  227. },
  228. /* common apis*/
  229. /*phone control api, only yealink support for now*/
  230. api_handfree: function (ext) {
  231. return this.send({action:"api", cmd:"api_handfree", args:{extn: ext}});
  232. },
  233. send : function(msg) {
  234. msg.uuid = ola.next_uuid();
  235. // log(JSON.stringify(msg));
  236. ola.ws.send(JSON.stringify(msg));
  237. return msg.uuid;
  238. },
  239. merge: function(target, additional){
  240. for (var i in additional) {
  241. if (additional.hasOwnProperty(i)) {
  242. target[i] = additional[i];
  243. }
  244. }
  245. },
  246. next_uuid : function() {
  247. var u = (parseFloat(ola.uuid.substring(29)) + 1).toString();
  248. u = u == "2147483647" ? "0" : u;
  249. while(u.length < 12) { u = "0" + u; }
  250. ola.uuid = "73836387-0000-0000-0000-0000-" + u;
  251. return ola.uuid;
  252. }
  253. };
  254. if ('jQuery' in this) jQuery.ola = this.ola;