You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.3 KiB

  1. # Copyright (C) 2018 Voicent
  2. # Copyright (C) 2019 Open Source Integrators
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. #
  5. # Documentation available at https://voicent.com/developer/docs/camp-api/
  6. import ast
  7. import csv
  8. import ntpath
  9. import os
  10. import requests
  11. class Voicent():
  12. def __init__(self, host="localhost", port="8155", callerid="000000000",
  13. line="1"):
  14. self.host_ = host
  15. self.port_ = port
  16. self.callerid_ = callerid
  17. self.line_ = line
  18. def postToGateway(self, urlstr, params, files=None):
  19. url = "http://" + self.host_ + ":" + self.port_ + urlstr
  20. res = requests.post(url, params, files=files)
  21. return res.text
  22. def getReqId(self, rcstr):
  23. index1 = rcstr.find("[ReqId=")
  24. if index1 == -1:
  25. return ""
  26. index1 += 7
  27. index2 = rcstr.find("]", index1)
  28. if index2 == -1:
  29. return ""
  30. return rcstr[index1:index2]
  31. def callText(self, phoneno, text, selfdelete):
  32. urlstr = "/ocall/callreqHandler.jsp"
  33. params = {
  34. 'info': 'simple text call',
  35. 'phoneno': phoneno,
  36. 'firstocc': 10,
  37. 'txt': text,
  38. 'selfdelete': selfdelete
  39. }
  40. res = self.postToGateway(urlstr, params)
  41. return self.getReqId(res)
  42. def callAudio(self, phoneno, filename, selfdelete):
  43. urlstr = "/ocall/callreqHandler.jsp"
  44. params = {
  45. 'info': 'simple audio call',
  46. 'phoneno': phoneno,
  47. 'firstocc': 10,
  48. 'audiofile': filename,
  49. 'selfdelete': selfdelete
  50. }
  51. res = self.postToGateway(urlstr, params)
  52. return self.getReqId(res)
  53. def callIvr(self, phoneno, appname, selfdelete):
  54. urlstr = "/ocall/callreqHandler.jsp"
  55. params = {
  56. 'info': 'simple text call',
  57. 'phoneno': phoneno,
  58. 'firstocc': 10,
  59. 'startapp': appname,
  60. 'selfdelete': selfdelete
  61. }
  62. res = self.postToGateway(urlstr, params)
  63. return self.getReqId(res)
  64. def callStatus(self, reqid):
  65. urlstr = "/ocall/callstatusHandler.jsp"
  66. params = {'reqid': reqid}
  67. res = self.postToGateway(urlstr, params)
  68. return self.getReqId(res)
  69. def callRemove(self, reqId):
  70. urlstr = "/ocall/callremoveHandler.jsp"
  71. params = {'reqid': reqId}
  72. res = self.postToGateway(urlstr, params)
  73. return self.getReqId(res)
  74. def callTillConfirm(self, vcastexe, vocfile, wavfile, ccode):
  75. urlstr = "/ocall/callreqHandler.jsp"
  76. cmdline = "\""
  77. cmdline += vocfile
  78. cmdline += "\""
  79. cmdline += " -startnow"
  80. cmdline += " -confirmcode "
  81. cmdline += ccode
  82. cmdline += " -wavfile "
  83. cmdline += "\""
  84. cmdline += wavfile
  85. cmdline += "\""
  86. params = {
  87. 'info': 'Simple Call till Confirm',
  88. 'phoneno': '1111111',
  89. 'firstocc': 10,
  90. 'selfdelete': 0,
  91. 'startexec': vcastexe,
  92. 'cmdline': cmdline
  93. }
  94. res = self.postToGateway(urlstr, params)
  95. return self.getReqId(res)
  96. ################
  97. # CAMPAIGN API #
  98. ################
  99. def importCampaign(self, listname, filepath):
  100. urlstr = "/ocall/campapi"
  101. params = {
  102. 'action': 'import',
  103. 'importfile': ntpath.basename(filepath),
  104. 'importfilepath': filepath,
  105. 'profile': 'Test',
  106. 'mod': 'cus',
  107. 'fieldsep': ',',
  108. 'row1': 1,
  109. 'mergeopt': 'empty',
  110. 'leadsrcname': listname
  111. }
  112. files = {
  113. 'file': (ntpath.basename(filepath), open(filepath, 'rb'))
  114. }
  115. res = self.postToGateway(urlstr, params, files)
  116. return ast.literal_eval(res)
  117. def runCampaign(self, listname):
  118. urlstr = "/ocall/campapi"
  119. params = {
  120. 'action': 'bbp',
  121. 'CAMP_NAME': 'Test',
  122. 'listname': listname,
  123. 'phonecols': 'Phone',
  124. 'lines': self.line_,
  125. 'calldisps': '',
  126. 'callerid': self.callerid_,
  127. }
  128. res = self.postToGateway(urlstr, params)
  129. return ast.literal_eval(res)
  130. def importAndRunCampaign(self, filepath, msgtype, msginfo):
  131. urlstr = "/ocall/campapi"
  132. params = {
  133. 'action': 'bbp',
  134. # Parameters for importing the campaign
  135. 'importfile': ntpath.basename(filepath),
  136. 'importfilepath': filepath,
  137. 'mergeopt': 'skip',
  138. # 'profile': 'Test',
  139. 'mod': 'cus',
  140. 'row1': 1,
  141. 'leadsrcname': 'Odoo Voicent Connector',
  142. # Parameters for running the campaign
  143. 'CAMP_NAME': 'Odoo Voicent Connector',
  144. 'phonecols': 'Phone',
  145. 'lines': self.line_,
  146. 'calldisps': '',
  147. 'callerid': self.callerid_,
  148. # Parameters for Autodialer
  149. 'msgtype': msgtype,
  150. 'msginfo': msginfo,
  151. }
  152. files = {
  153. 'file': (ntpath.basename(filepath), open(filepath, 'rb'))
  154. }
  155. res = self.postToGateway(urlstr, params, files)
  156. return ast.literal_eval(res)
  157. def checkStatus(self, camp_id):
  158. urlstr = "/ocall/campapi"
  159. params = {
  160. 'action': 'campstats',
  161. 'camp_id': camp_id
  162. }
  163. res = self.postToGateway(urlstr, params)
  164. return ast.literal_eval(res)
  165. def exportResult(self, camp_id, filename, extracols=None):
  166. urlstr = "/ocall/campapi"
  167. params = {
  168. 'action': 'exportcamp',
  169. 'camp_id': camp_id,
  170. 'f': 'webapps/ROOT/assets/global/' + filename,
  171. 'extracols': extracols
  172. }
  173. res = ast.literal_eval(self.postToGateway(urlstr, params))
  174. if res.get('status') == 'OK':
  175. url = 'http://' + self.host_ + ':' + self.port_ + \
  176. '/assets/global/' + filename
  177. res2 = requests.get(url)
  178. with open(filename, 'wb') as local_file:
  179. local_file.write(res2.content)
  180. local_file.close()
  181. reader = csv.DictReader(open(filename, 'r'))
  182. os.remove(filename)
  183. for row in reader:
  184. return row
  185. else:
  186. return res