clickatell http库
python-clickatell的Python项目详细描述
Bellville,Clickatell Python库
python clickatell http库
>>> from clickatell.api import Clickatell >>> from clickatell import constants as cc >>> clickatell = Clickatell('username','password','api_id', ... sendmsg_defaults={ ... 'callback': cc.YES, ... 'msg_type': cc.SMS_DEFAULT, ... 'deliv_ack': cc.YES, ... 'req_feat': cc.FEAT_ALPHA + \ ... cc.FEAT_NUMER + \ ... cc.FEAT_DELIVACK ... }) >>> clickatell.sendmsg(recipients=['27123456789'], \ ... sender='27123456789', text='hello world') [ERR: 301, No Credit Left] >>>
下一步,为您的clickatell帐户获得一些积分。
开始
确保你在clickatell有一个帐户,他们会给你10个免费的sms,你可以用来测试。但是,即使是未送达的邮件也会计入您的信用,如果您计划实际使用10,请确保使用库100%正确。使用他们提供的用户名、密码和API ID。
如果您想使用该代码,则以下内容就足够了
$ python setup.py install
如果要开发代码,请执行以下操作:
$ virtualenv --no-site-packages ve/ $ source ve/bin/activate (ve)$ pip -E ve/ install -r requirements.pip (ve)$ python setup.py develop ...
来自src/的clickatell库将位于pythonpath上,并可通过python shell访问。
测试
此项目使用nose进行测试,主分支应该始终稳定:
(ve)$ pip install nose (ve)$ nosetests ...
使用贝尔维尔
一些代码示例说明了这件事是如何工作的。
发送一条短信
>>> from clickatell.api import Clickatell >>> clickatell = Clickatell('username','password','api_id') >>> [resp] = clickatell.sendmsg(recipients=['27123456789'], ... sender='27123456789', ... text='hello world') >>> resp IDResponse: ce7f181a44a4a5b7e43fe2b9a0b1f0c1 >>> resp.value # Clickatell's apiMsgId value 'ce7f181a44a4a5b7e43fe2b9a0b1f0c1' >>>
向多个收件人发送短信
>>> [resp1, resp2] = clickatell.sendmsg(recipients=[ ... '27123456781', ... '27123456782'], ... sender='27123456789', ... text='hello world') >>> resp1.value # the apiMsgId 'ce7f181a44a4a5b7e43fe2b9a0b1f0c1' >>> resp1.extra # the extra values returned for the response {'To': '27123456781'} >>> resp2.value 'ce7f181a44a4a5b7e43fe2b9a0b1f0c2' >>> resp2.extra {'To': '27123456782'} >>>
检查消息的状态
clickatell允许您通过轮询邮件的服务器来检查邮件的状态。但是,它们也允许您使用http回调,在那里它们会将状态实时发布到您的服务器。这样更快更高效。
>>> status = clickatell.querymsg( \ ... apimsgid='ce7f181a44a4a5b7e43fe2b9a0b1f0c1') >>> status.value 'ce7f181a44a4a5b7e43fe2b9a0b1f0c1' >>> status.extra {'Status': '002'} >>>
检查clickatell帐户的余额
>>> clickatell.getbalance() 0.67000000000000004 >>>
检查msisdn
的覆盖范围>>> clickatell.check_coverage('27219107700') ERRResponse: This prefix is not currently supported. Messages sent to this prefix will fail. Please contact support for assistance. >>> resp = clickatell.check_coverage('2776*******') >>> resp OKResponse: This prefix is currently supported. Messages sent to this prefix will be routed. Charge: 1 >>> resp.value 'This prefix is currently supported. Messages sent to this prefix will be routed.' >>> resp.extra {'Charge': '1'} >>>
检查消息费用
>>> resp = clickatell.getmsgcharge( \ apimsgid='ce7f181a44a4a5b7e43fe2b9a0b1f0c1') >>> resp.value 'ce7f181a44a4a5b7e43fe2b9a0b1f0c1' >>> resp.extra {'status': '002', 'charge': '1'} >>>
发送成批消息
来自batch.sendmsg()方法的响应与来自clickatell.sendmsg()的响应相同。
>>> batch = clickatell.batch(sender='27123456789', ... template='Hello #field1# #field2#') >>> with batch: ... batch.sendmsg(to='27123456781', context={ ... 'field1': 'Foo 1', ... 'field2':'Bar 1' ... }) ... batch.sendmsg(to='27123456782', context={ ... 'field1': 'Foo 2', ... 'field2':'Bar 2' ... }) ... ERRResponse: 301, No Credit Left ERRResponse: 301, No Credit Left >>> # shucks
要使with语句工作,您需要使用Python2.6或更高版本。如果使用以下命令手动启用它,则它可以在Python2.5中工作:
>>> from __future__ import with_statement
如果您不想使用上下文管理器,可以使用batch-id参数手动调用batch.start()和batch.end()。
>>> batch = clickatell.batch(sender='27123456789', ... template='Hello #field1# #field2#') >>> batch_id = batch.start() >>> batch.sendmsg(to='...', batch_id=batch_id, context={...}) >>> batch.sendmsg(to='...', batch_id=batch_id, context={...}) >>> batch.sendmsg(to='...', batch_id=batch_id, context={...}) >>> batch.end(batch_id)
向多个收件人发送快速消息:
>>> with clickatell.batch(sender='27123456789', ... template='Hello world!') as batch: ... [apimsgid1, apimsgid2, apimsgid3] = batch.quicksend(recipients=[ ... '27123456781', ... '27123456782', ... '27123456783', ... ]) ... >>> apimsgid1 ERRResponse: 301, No Credit Left To: 27123456781 >>> apimsgid2 ERRResponse: 301, No Credit Left To: 27123456782 >>> apimsgid3 ERRResponse: 301, No Credit Left To: 27123456783 >>>
待办事项:
尚未实现的内容是:
- deletion of queued messages
- MMS push
- WAP push service indication
- Token voucher payment
- 8bit messaging