voximplant api客户端库
voximplant-apiclient的Python项目详细描述
先决条件
为了使用Vox植入Python SDK,您需要如下:
- 开发人员帐户。如果你没有的话,sign up here。
- 一个私有api密钥。有两个选项可供选择:
- 在Voximplant Control panel 中生成它
- 或者使用指定的authentication parameters调用CreateKeyhttp api方法。您将收到一个包含result字段的响应。将result值保存在文件中(因为我们不存储密钥,所以请将其安全地保存在您的一侧)。
- python 2.x或3.x运行时,安装了pip和setuptools>;=18.5
如何使用
转到项目文件夹并使用pip:
安装sdkpython -m pip install --user voximplant-apiclient
然后在脚本中导入sdk
fromvoximplant.apiclientimportVoximplantAPI
接下来,在构造函数中或使用环境中指定json服务帐户文件的路径。
构造函数:
api=VoximplantAPI("/path/to/credentials.json")
env:
exportVOXIMPLANT_CREDENTIALS=/path/to/credentials.json
示例
开始一个场景
fromvoximplant.apiclientimportVoximplantAPI,VoximplantExceptionif__name__=="__main__":api=VoximplantAPI("credentials.json")# Start a scenario of the user 1RULE_ID=1SCRIPT_CUSTOM_DATA="mystr"USER_ID=1try:res=api.start_scenarios(RULE_ID,script_custom_data=SCRIPT_CUSTOM_DATA,user_id=USER_ID)print(res)exceptVoximplantExceptionase:print("Error: {}".format(e.message))
发送短信
fromvoximplant.apiclientimportVoximplantAPI,VoximplantExceptionif__name__=="__main__":api=VoximplantAPI("credentials.json")# Send the SMS with the "Test message" text from the phone number 447443332211 to the phone number 447443332212SOURCE="447443332211"DESTINATION="447443332212"SMS_BODY="Test message"try:res=api.send_sms_message(SOURCE,DESTINATION,SMS_BODY)print(res)exceptVoximplantExceptionase:print("Error: {}".format(e.message))
获取通话记录项
fromvoximplant.apiclientimportVoximplantAPI,VoximplantExceptionimportpytzimportdatetimeif__name__=="__main__":api=VoximplantAPI("credentials.json")# Get the first call session history record from the 2012-01-01 00:00:00 UTC to the 2014-01-01 00:00:00 UTCFROM_DATE=datetime.datetime(2012,1,1,0,0,0,pytz.utc)TO_DATE=datetime.datetime(2014,1,1,0,0,0,pytz.utc)COUNT=1try:res=api.get_call_history(FROM_DATE,TO_DATE,count=COUNT)print(res)exceptVoximplantExceptionase:print("Error: {}".format(e.message))