Skip to main content

qcloudsms python sdk

Project description

腾讯云短信 Python SDK
===


# Overview

> 目前腾讯云短信为客户提供国内短信、海外短信和语音通知三大服务。

> 国内短信提供单发、群发、带模板ID单发、带模板ID群发以及短信回执与回复拉取。

> 海外短信和国内短信使用同一接口,只需替换相应的国家码与手机号码,每次请求群发接口手机号码需全部为国内或者海外手机号码。

> 语音通知目前支持语音验证码和语音通知功能。


# Getting Start

## 准备

- [ ] 申请APPID以及APPKey

> 在开始本教程之前,您需要先获取APPID和APPkey,如您尚未申请,请到https://console.qcloud.com/sms/smslist 中添加应用,应用添加成功后您将获得APPID以及APPKey,注意APPID是以14xxxxx开头。

- [ ] 申请签名

> 下发短信必须携带签名,在相应服务模块 *短信内容配置* 中进行申请。

- [ ] 申请模板

> 下发短信内容必须经过审核,在相应服务 *短信内容配置* 中进行申请。

完成以上三项便可开始代码开发。

## 安装

### pip

qcloudsms_py采用pip进行安装,要使用qcloudsms功能, 只需要执行:

```shell
pip install qcloudsms_py
```

### 手动

1. 手动下载或clone最新版本qcloudsms_py代码
2. 在qcloudsms_py目录运行 `python setup.py install`或直接把qcloudsms_py所在目录加入`sys.path`

## 用法

> 若您对接口存在疑问,可以查阅[API开发指南](https://cloud.tencent.com/document/product/382/5808)和[API文档](https://qcloudsms.github.io/qcloudsms_py/)。

- **准备必要参数**

```python
appid = 122333333;
appkey = "111111111112132312xx";
phone_numbers = ["21212313123", "12345678902", "12345678903"];
template_id = 7839;
```

- **单发短信**

```python
from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError

ssender = SmsSingleSender(appid, appkey)
try:
result = ssender.send(0, 86, phone_numbers[0],
"测试短信,普通单发,深圳,小明,上学。")
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(result)
```

> `Note`: 发送短信没有指定模板ID时,发送的内容需要与已审核通过的模板内容相匹配,才可能下发成功,否则返回失败。
> `Note`: 如需发送海外短信,同样可以使用此接口,只需将国家码"86"改写成对应国家码号。

- **指定模板ID单发短信**

```python
from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError

ssender = SmsSingleSender(appid, appkey)
params = ["指定模板单发", "深圳", "小明"]
try:
result = ssender.send_with_param(86, phone_numbers[0],
template_id, params)
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(result)
```

> `Note:`无论单发短信还是指定模板ID单发短信都需要从控制台中申请模板并且模板已经审核通过,才可能下发成功,否则返回失败。

- **群发**

```python
from qcloudsms_py import SmsMultiSender
from qcloudsms_py.httpclient import HTTPError

msender = SmsMultiSender(appid, appkey)
try:
result = msender.send(0, "86", phone_numbers,
"测试短信,普通群发,深圳,小明,上学。")
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(result)
```

- **指定模板ID群发**

```python
from qcloudsms_py import SmsMultiSender
from qcloudsms_py.httpclient import HTTPError

msender = SmsMultiSender(appid, appkey)
params = ["指定模板群发", "深圳", "小明"]
try:
result = msender.send_with_param(86, phone_numbers[0],
template_id, params)
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(result)
```

> `Note:`群发一次请求最多支持200个号码,如有对号码数量有特殊需求请联系腾讯云短信技术支持(QQ:3012203387)。

- **发送语音验证码**

```python
from qcloudsms_py import SmsVoiceVerifyCodeSender
from qcloudsms_py.httpclient import HTTPError

vvcsender = SmsVoiceVerifyCodeSender(appid, appkey)
try:
result = vvcsender.send("86", phone_numbers[0], "1234", 2)
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(result)
```

> `Note`: 语音验证码发送只需提供验证码数字,例如msg内容为“123”时,您收到的语音通知为“您的语音验证码是123,如需自定义内容,可以使用语音通知。

- **发送语音通知**

```python
from qcloudsms_py import SmsVoicePromptSender
from qcloudsms_py.httpclient import HTTPError


vpsender = SmsVoicePromptSender(appid, appkey)
try:
result = vpsender.send("86", phone_numbers[0], 2, "1234", 2)
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(result)
```

- **拉取短信回执以及回复**

```python
from qcloudsms_py import SmsStatusPuller
from qcloudsms_py.httpclient import HTTPError

spuller = SmsStatusPuller(appid, appkey)
try:
# 拉取短信回执
callback_result = spuller.pull_callback(10);
# 拉取回复
reply_result = spuller.pull_reply(10);
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(callback_result)
print(reply_result)
```

> `Note:` 短信拉取功能需要联系腾讯云短信技术支持(QQ:3012203387),量大客户可以使用此功能批量拉取,其他客户不建议使用。

- **拉取单个手机短信状态**

```python
from qcloudsms_py import SmsMobileStatusPuller
from qcloudsms_py.httpclient import HTTPError

mspuller = SmsMobileStatusPuller(appid, appkey)
try:
# 拉取短信回执
callback_result = mspuller.pull_callback("86", phone_numbers[0],
1511125600, 1511841600, 10)
# 拉取回复
reply_result = mspuller.pull_reply("86", phone_numbers[0],
1511125600, 1511841600, 10)
except HTTPError as e:
print(e)
except Exception as e:
print(e)

print(callback_result)
print(reply_result)
```

- **发送海外短信**

海外短信与国内短信发送类似

Project details


Release history Release notifications

History Node

0.1.3

History Node

0.1.1

This version
History Node

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
qcloudsms_py-0.1.0.tar.gz (43.1 kB) Copy SHA256 hash SHA256 Source None Dec 7, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page