对比官方文档,再次进行封装了一下

未完善

先pip install qcloudsms_py

python duitang.py -i ug10_cn -m "服崩溃了啊。。dev_s1"

电话列表可以修改一下自己想要的。-m 接的是你想要讲的信息,end

#!/usr/bin/python3
import getopt
import sys
from qcloudsms_py import SmsVoiceVerifyCodeSender
import datetime,time
from datetime import datetime as dtime

# 语音消息应用 SDK AppID
appid = 1666666  # SDK AppID 以1400开头
# 语音消息应用 App Key
appkey = "4558494666666"
# 需要发打电话消息的手机号码 可批量
phone_numbers = {
    'ub02_cn': [19888888888, 19888888888, 1372991988888888875787],
    'ug10_cn': [19888888888],

}

def usage():
    print('--- 使用错误: {} -i <ug03_cn> -m <message> '.format(sys.argv[0]))

### 验证码可以再传参,不写了
def call_phone(fork, message):
    # 获取周几
    dayOfWeek = dtime.now().isoweekday()
    # 上午十点
    start_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '10:00', '%Y-%m-%d%H:%M')  # 10点00开始
    start_time_array = time.strptime(str(start_time), "%Y-%m-%d %H:%M:%S")
    start_time_ts = int(time.mktime(start_time_array))
    # 晚上八点半
    end_time = datetime.datetime.strptime(str(datetime.datetime.now().date()) + '20:30', '%Y-%m-%d%H:%M')  # 20点30结束
    end_time_array = time.strptime(str(end_time), "%Y-%m-%d %H:%M:%S")
    end_time_ts = int(time.mktime(end_time_array))
    # 当前时间
    now_time = int(time.time())

    # 语音模板 ID,需要在语音消息控制台中申请
    template_id = 7839  # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在语音消息控制台中申请
    vvcsender = SmsVoiceVerifyCodeSender(appid, appkey)

    # 周六日 全天候call电话 工作日上班时间外call
    if dayOfWeek == 6 or dayOfWeek == 7:
        for key in phone_numbers:
            if key == fork:
                for num in phone_numbers[key]:
                    print(key, "电话是:", num, "消息是:", message)
                    result = vvcsender.send("86", num, "6666", playtimes=2, ext="")
                    time.sleep(3)
                    print(result)
        return "周末,打电话"
    else:
        if now_time >= start_time_ts and now_time <= end_time_ts:
            return "上班时间,不打电话!"
        else:
            for key in phone_numbers:
                if key == fork:
                    for num in phone_numbers[key]:
                        print(key, "电话是:", num, "消息是:", message)
                        result = vvcsender.send("86", num, "6666", playtimes=2, ext="")
                        time.sleep(3)
                        print(result)
            return "下班时间,打电话"


def run():
    fork = ''
    message = ''
    try:
        options, args = getopt.getopt(sys.argv[1:], "hi:m:", longopts=['help', 'fork=', 'message='])
    except getopt.GetoptError:
        usage()
        sys.exit(3)
    if not (options or args):
        usage()
        sys.exit(3)
    for opt, arg in options:
        if opt in ('-h', '--help'):
            usage()
        if opt in ('-i', '--fork'):
            fork = arg
        if opt in ('-m', '--message'):
            message = arg

    if fork and message:
        text = call_phone(fork, message)
        print(text)


if __name__ == '__main__':
    run()

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://kkxl95.cn/article/1609849671/