用于python的nexmo客户端库
nexmo的Python项目详细描述
用于python的nexmo客户端库
这是nexmo的api的python客户端库。使用它你会 需要一个nexmo帐户。注册for free at nexmo.com。
安装
使用pip安装python客户端库:
pip install nexmo
要使用pip升级已安装的客户端库:
pip install nexmo --upgrade
或者,您可以通过命令行克隆存储库:
git clone git@github.com:Nexmo/nexmo-python.git
或者在github桌面上打开它。
用法
首先导入nexmo
模块:
importnexmo
然后用密钥和密钥构造一个客户端对象:
client=nexmo.Client(key=api_key,secret=api_secret)
对于生产,可以指定NEXMO_API_KEY
和NEXMO_API_SECRET
。
环境变量,而不是显式指定密钥和机密。
对于支持jwt身份验证的较新端点,如语音api,
您还可以指定application_id
和private_key
参数:
client=nexmo.Client(application_id=application_id,private_key=private_key)
要检查传入webhook请求的签名,还需要
指定signature_secret
参数(或NEXMO_SIGNATURE_SECRET
环境变量)。
短信API
发送短信
response=client.send_message({'from':'Python','to':'YOUR-NUMBER','text':'Hello world'})response=response['messages'][0]ifresponse['status']=='0':print('Sent message',response['message-id'])print('Remaining balance is',response['remaining-balance'])else:print('Error:',response['error-text'])
文件:https://developer.nexmo.com/api/sms#send-an-sms
告诉nexmo收到短信
下面提交一个具有当前时间戳的成功转换到nexmo。此功能必须 首先在您的帐户上启用。
response=client.submit_sms_conversion(message_id)
语音API
打电话
response=client.create_call({'to':[{'type':'phone','number':'14843331234'}],'from':{'type':'phone','number':'14843335555'},'answer_url':['https://example.com/answer']})
文件:https://developer.nexmo.com/api/voice#createCall
检索呼叫列表
response=client.get_calls()
文件:https://developer.nexmo.com/api/voice#getCalls
检索单个调用
response=client.get_call(uuid)
文件:https://developer.nexmo.com/api/voice#getCall
更新呼叫
response=client.update_call(uuid,action='hangup')
文件:https://developer.nexmo.com/api/voice#updateCall
将音频流传输到呼叫
stream_url='https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3'response=client.send_audio(uuid,stream_url=[stream_url])
文件:https://developer.nexmo.com/api/voice#startStream
停止将音频流传输到呼叫
response=client.stop_audio(uuid)
文件:https://developer.nexmo.com/api/voice#stopStream
向呼叫发送合成语音消息
response=client.send_speech(uuid,text='Hello')
文件:https://developer.nexmo.com/api/voice#startTalk
停止向呼叫发送合成语音消息
response=client.stop_speech(uuid)
文件:https://developer.nexmo.com/api/voice#stopTalk
向通话发送DTMF铃声
response=client.send_dtmf(uuid,digits='1234')
文件:https://developer.nexmo.com/api/voice#startDTMF
获取录制
response=client.get_recording(RECORDING_URL)
验证API
开始验证
response=client.start_verification(number='441632960960',brand='MyApp')ifresponse['status']=='0':print('Started verification request_id={request_id}'.format(request_id=response['request_id']))else:print('Error:',response['error_text'])
文件:https://developer.nexmo.com/api/verify#verify-request
响应包含一个验证请求id,您需要 临时存储(在会话、数据库、url等中)。
检查验证
response=client.check_verification('00e6c3377e5348cdaf567e1417c707a5',code='1234')ifresponse['status']=='0':print('Verification complete, event_id={event_id}'.format(event_id=response['event_id']))else:print('Error:',response['error_text'])
文件:https://developer.nexmo.com/api/verify#verify-check
验证请求id来自对start_验证方法的调用。 PIN码由用户输入到您的应用程序中。
取消验证
client.cancel_verification('00e6c3377e5348cdaf567e1417c707a5')
文件:https://developer.nexmo.com/api/verify#verify-control
触发下一个验证步骤
client.trigger_next_verification_event('00e6c3377e5348cdaf567e1417c707a5')
文件:https://developer.nexmo.com/api/verify#verify-control
数字洞察API
基本数字洞察力
client.get_basic_number_insight(number='447700900000')
文件:https://developer.nexmo.com/api/number-insight#getNumberInsightBasic
标准号码洞察
client.get_standard_number_insight(number='447700900000')
文件:https://developer.nexmo.com/api/number-insight#getNumberInsightStandard
高级数字洞察力
client.get_advanced_number_insight(number='447700900000')
文件:https://developer.nexmo.com/api/number-insight#getNumberInsightAdvanced
管理机密
提供了一个api,允许您旋转api机密。您可以创建一个新的秘密(最多有两个秘密),并且在所有应用程序都被更新后删除现有的一个秘密。
列出秘密
secrets=client.list_secrets(API_KEY)
创建新的秘密
创建一个新的机密(创建的日期将帮助您知道哪个是哪个):
client.create_secret(API_KEY,'awes0meNewSekret!!;');
删除机密
删除旧机密(任何仍使用这些凭据的应用程序将停止工作):
client.delete_secret(API_KEY,'my-secret-id')
应用程序API
创建应用程序
response=client.application_v2.create_application({name='Example App',type='voice'})
文件:https://developer.nexmo.com/api/application.v2#createApplication
检索应用程序列表
response=client.application_v2.list_applications()
文件:https://developer.nexmo.com/api/application.v2#listApplication
检索单个应用程序
response=client.application_v2.get_application(uuid)
文件:https://developer.nexmo.com/api/application.v2#getApplication
更新应用程序
response=client.application_v2.update_application(uuid,answer_method='POST')
文件:https://developer.nexmo.com/api/application.v2#updateApplication
删除应用程序
response=client.application_v2.delete_application(uuid)
文件:https://developer.nexmo.com/api/application.v2#deleteApplication
验证Webhook签名
client=nexmo.Client(signature_secret='secret')ifclient.check_signature(request.query):# valid signatureelse:# invalid signature
文件:https://developer.nexmo.com/concepts/guides/signing-messages
注意:您需要联系support@nexmo.com才能启用消息登录 您以前的帐户您可以验证webhook签名。
JWT参数
默认情况下,库生成用于jwt身份验证的短期令牌。
使用auth方法指定长寿命令牌的参数或 指定其他令牌标识符:
client.auth(nbf=nbf,exp=exp,jti=jti)
贡献
我们:心:贡献!但如果你打算做一些大的或有争议的事情,请先contact us!
我们建议使用virtualenv处理nexmo-python
。以下命令将安装运行测试所需的所有python依赖项:
make install
这些测试都是用pytest编写的。您可以使用:
make test
许可证
这个库是在MIT License下发布的。