阿里雲短信 以及 企業微信 python接口

目前相關模塊已經同步至全部機器,對應python版本3。 html

python包依賴:click (pip3 install click)python

源碼地址
阿里雲短信:/usr/local/python36/lib/python3.6/site-packages/send_sms/sendsms.py,依賴兩個阿里提供的sdk
/usr/local/python36/lib/python3.6/site-packages/aliyunsdkcore 和
/usr/local/python36/lib/python3.6/site-packages/aliyunsdkdysmsapic#

企業微信: /usr/local/python36/lib/python3.6/site-packages/weixin_qiye.pyapi

特色:均支持代碼中引用以及命令行調用兩種使用方式微信

  1. 在python代碼中經過模塊引入方式調用
    阿里雲短信ide

    >>> from send_sms import sendsms
    >>> sendsms.
    sendsms.AcsClient(       sendsms.PRODUCT_NAME     sendsms.SendSmsRequest   sendsms.const            sendsms.send_sms(        sendsms.uuid
    sendsms.DOMAIN           sendsms.REGION           sendsms.acs_client       sendsms.region_provider  sendsms.sys         
    
    >>> help(sendsms.send_sms)
    Help on function send_sms in module send_sms.sendsms:
    send_sms(phone_numbers, sign_name, template_code, template_param=None)
    phone_numbers: String, 接收短信的手機號碼, 支持對多個手機號碼發送短信,手機號碼之間以英文逗號(,)分隔。上限爲1000個手機號碼。批量調用相對於單條調用及時性稍有延遲
    sign_name: String, 短信簽名名稱。請在控制檯簽名管理頁面簽名名稱一列查看
    template_code: String, 短信模板ID。請在控制檯模板管理頁面模板CODE一列查看
    template_param: String, 短信模板變量對應的實際值,JSON格式
    
    >>> phone_numbers = '15810xxxxxx'
    >>> sign_name = '公司'
    >>> template_code = 'SMS_14159xxxx'
    >>> template_param = '{"sysName":"nongye","modName":"","alarmInfo":"ljk test"}'
    >>> sendsms.send_sms(phone_numbers, sign_name, template_code, template_param).decode()
    '{"Message":"OK","RequestId":"5D9C9B11-A4C1-43B1-BCC9-2B2F0EBD4170","BizId":"920009151147346098^0","Code":"OK"}'

    企業微信ui

    import weixin_qiye
    
    >>> help(weixin_qiye.gettoken)
    Help on function gettoken in module weixin_qiye:
    gettoken(corpid, corpsecret)
    獲取access_token
    corpid: 企業ID
    corpsecret: 應用的憑證密鑰
    
    >>> help(weixin_qiye.senddata)
    Help on function senddata in module weixin_qiye:
    senddata(access_token, msg, touser='', toparty='', agentid=1)
    經過企業微信發送文本消息
    access_token: 調用接口憑證
    msg: 文本消息
    touser: 成員ID列表(消息接收者,多個接收者用‘|’分隔,最多支持1000個)。特殊狀況:指定爲@all,則向該企業應用的所有成員發送
    toparty: 部門ID列表,多個接收者用‘|’分隔,最多支持100個。當touser爲@all時忽略本參數
    agentid: 企業應用的id
    
    >>> token = weixin_qiye.gettoken('wx14f4b31', 'JynHsZm-pcuAel1ruG-5mbktKQea')
    >>> weixin_qiye.senddata(token, 'ljk test', 'LiKai')
    {'errcode': 0, 'errmsg': 'ok', 'invaliduser': ''}
  2. 做爲腳本在Linux命令行運行 this

    阿里雲短信阿里雲

    ins01 ~]# cd /usr/local/python36/lib/python3.6/site-packages
    ins01 site-packages]# python3 send_sms/sendsms.py --help
    Usage: sendsms.py [OPTIONS]
    
    Options:
      --phone_numbers TEXT   接收短信的手機號碼, 支持對多個手機號碼發送短信,手機號碼之間以英文逗號(,)分隔。上限爲1000個手機號
                         碼。批量調用相對於單條調用及時性稍有延遲  [required]
      --sign_name TEXT       短信簽名名稱。請在控制檯簽名管理頁面簽名名稱一列查看  [required]
      --template_code TEXT   短信模板ID。請在控制檯模板管理頁面模板CODE一列查看  [required]
      --template_param TEXT  短信模板變量對應的實際值,JSON格式  [required]
      --help                 Show this message and exit.
    
    ins01 site-packages]# python3 send_sms/sendsms.py --phone_numbers 15810xxxxxx --sign_name 公司 --template_code SMS_14159xxxx --template_param '{"sysName":"nongye","modName":"","alarmInfo":"ljk test"}'
    {"Message":"OK","RequestId":"543E6ACF-5CDD-4BF7-B2D1-671FA45A3","BizId":"513316751727947^0","Code":"OK"}

    企業微信命令行

    ins01 site-packages]# python3 weixin_qiye.py --help
    Usage: weixin_qiye.py [OPTIONS] COMMAND [ARGS]...
    
    Options:
      --help  Show this message and exit.
    
    Commands:
      gettoken  獲取access_token
      senddata  經過企業微信發送文本消息
    
    ins01 site-packages]# python3 weixin_qiye.py gettoken --help
    Usage: weixin_qiye.py gettoken [OPTIONS]
    
      獲取access_token
    
    Options:
      --corpid TEXT      企業ID  [required]
      --corpsecret TEXT  應用的憑證密鑰  [required]
      --help             Show this message and exit.
    
    ins01 site-packages]# token=`python3 weixin_qiye.py gettoken --corpid wx4f4bbb --corpsecret JyryvHsZm-cuql1ruG-5mbktKJMntavh`
    ins01 site-packages]# python3 weixin_qiye.py senddata --access_token $token --msg 'test 1122334455' --touser='LiKai'
    {'errcode': 0, 'errmsg': 'ok', 'invaliduser': ''}

代碼請見這兒

相關文章
相關標籤/搜索