Просмотр исходного кода

Attended transfer draft implementation

Hal De 4 лет назад
Родитель
Сommit
7c493ffb57
1 измененных файлов с 39 добавлено и 0 удалено
  1. 39 0
      app/app.py

+ 39 - 0
app/app.py

@@ -343,6 +343,16 @@ async def amiQueues():
         queues.setdefault(message.queue, []).append(_qm.fromMessage(message))
   return queues
 
+async def amiDeviceChannel(device):
+  reply = await manager.send_action({'Action':'CoreShowChannels'})
+  if len(reply) >= 2:
+    for message in reply:
+      if message.event == 'CoreShowChannel':
+        print(message.calleridnum)
+        if message.calleridnum == device:
+          return message.channel
+  return None
+
 async def setQueueStates(queues, user, device, state):
   for queue in [_q for _q, _ma in queues.items() for _m in _ma if _m.user == user]:
     await amiSetVar('DEVICE_STATE(Custom:QUEUE{}*{})'.format(device, queue), state)
@@ -359,6 +369,9 @@ async def getUserCID(user):
 async def setDeviceUser(device, user):
   return await amiDBPut('DEVICE', '{}/user'.format(device), user)
 
+async def getUserDevice(user):
+  return await amiDBGet('AMPUSER', '{}/device'.format(user))
+
 async def setUserDevice(user, device):
   if device is None:
     return await amiDBDel('AMPUSER', '{}/device'.format(user))
@@ -411,6 +424,32 @@ async def getUsersStates():
       states[user] = uState
   return states
 
+@app.route('/atxfer/<userA>/<userB>')
+class AtXfer(Resource):
+  @app.param('userA', 'User initiating the attended transfer', 'path')
+  @app.param('userB', 'Transfer destination user', 'path')
+  @app.response(HTTPStatus.OK, 'Successfuly transfered the call')
+  @app.response(HTTPStatus.UNAUTHORIZED, 'Authorization required')
+  @app.response(HTTPStatus.NOT_FOUND, 'User does not exist, or user is not bound to device, \
+                                       or device is offline, or no current call for user')
+  @app.response(HTTPStatus.BAD_REQUEST, 'Transfer failed somehow or other AMI error')
+  async def get(self, userA, userB):
+    '''Attended call transfer
+    '''
+    device = await getUserDevice(userA)
+    if device in NONEs:
+      return '', HTTPStatus.NOT_FOUND
+    channel = await amiDeviceChannel(device)
+    if channel in NONEs:
+      return '', HTTPStatus.NOT_FOUND
+    reply = await manager.send_action({'Action':'Atxfer',
+                                       'Channel':channel,
+                                       'async':'false',
+                                       'Exten':userB})
+    if (isinstance(reply, Message) and reply.success):
+      return '', HTTPStatus.OK
+    return '', HTTPStatus.BAD_REQUEST
+
 @app.route('/users/states')
 class UsersStates(Resource):
   @app.response(HTTPStatus.OK, 'JSON map of form {user1:state1, user2:state2, ...}')