Skip to content

Instantly share code, notes, and snippets.

@lyymee
Forked from dylan4224/alisms.py
Created February 21, 2019 14:03
Show Gist options
  • Select an option

  • Save lyymee/d3b780e614064b24d52ffb45812c4f9c to your computer and use it in GitHub Desktop.

Select an option

Save lyymee/d3b780e614064b24d52ffb45812c4f9c to your computer and use it in GitHub Desktop.

Revisions

  1. @dylan4224 dylan4224 renamed this gist Dec 16, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions alidayu.py → alisms.py
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ def sign(text, secret):
    return signture


    class AliDaYuSMS(object):
    class AliSMS(object):

    _defaults = [
    ('action', 'SendSms'),
    @@ -99,6 +99,6 @@ def _create_params(self, phone, template_code, template_params):
    template_params = {
    'code': 'your_code'
    }
    sms = AliDaYuSMS(app_key, app_secret, sign_name)
    sms = AliSMS(app_key, app_secret, sign_name)
    resp = sms.send('the_phone_number', template_code, template_params)
    print(resp.status_code, resp.json())
  2. @dylan4224 dylan4224 revised this gist Dec 16, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions alidayu.py
    Original file line number Diff line number Diff line change
    @@ -97,8 +97,8 @@ def _create_params(self, phone, template_code, template_params):
    sign_name = 'your_sign_name'
    template_code = 'your_template_code:SMS_123456789'
    template_params = {
    'code': '1024'
    'code': 'your_code'
    }
    sms = AliDaYuSMS(app_key, app_secret, sign_name)
    resp = sms.send('phone_number', template_code, template_params)
    resp = sms.send('the_phone_number', template_code, template_params)
    print(resp.status_code, resp.json())
  3. @dylan4224 dylan4224 revised this gist Dec 16, 2017. No changes.
  4. @dylan4224 dylan4224 revised this gist Dec 16, 2017. No changes.
  5. @dylan4224 dylan4224 created this gist Dec 16, 2017.
    104 changes: 104 additions & 0 deletions alidayu.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import base64
    import datetime
    import hmac
    import json
    import urllib
    import uuid

    import requests


    def quote(text):
    return urllib.parse.quote(text, safe='~')


    def stringify(**kwargs):
    pairs = []
    for k, v in sorted(kwargs.items()):
    pairs.append('{}={}'.format(k, v))
    return '&'.join(pairs)


    def canonicalize(**kwargs):
    pairs = []
    for k, v in sorted(kwargs.items()):
    pair = '{}={}'.format(quote(k), quote(v))
    pairs.append(pair)
    return quote('&'.join(pairs))


    def sign(text, secret):
    text = text.encode('utf-8')
    key = (secret + '&').encode('utf-8')
    digest = hmac.new(key, text, 'sha1').digest()
    signture = quote(base64.b64encode(digest))
    return signture


    class AliDaYuSMS(object):

    _defaults = [
    ('action', 'SendSms'),
    ('format', 'JSON'),
    ('region_id', 'cn-hangzhou'),
    ('signature_method', 'HMAC-SHA1'),
    ('signature_version', '1.0'),
    ('sms_version', '2017-05-25'),
    ('domain', 'https://dysmsapi.aliyuncs.com'),
    ]

    def __init__(self, app_key, app_secret, sign_name, **settings):
    for k, v in self._defaults:
    setattr(self, k, settings.get(k, v))

    self.app_key = app_key
    self.app_secret = app_secret
    self.sign_name = sign_name

    def send(self, phone, template_code, template_params):
    body = self._create_body(phone, template_code, template_params)
    headers = {
    'content-type': 'application/x-www-form-urlencoded',
    }
    res = requests.post(self.domain, data=body, headers=headers)
    return res

    def _create_body(self, phone, template_code, template_params):
    params = self._create_params(phone, template_code, template_params)
    text = 'POST&%2F&' + canonicalize(**params)
    signture = sign(text, self.app_secret)
    body = 'Signature={}&{}'.format(signture, stringify(**params))
    return body.encode('utf-8')

    def _create_params(self, phone, template_code, template_params):
    return {
    'AccessKeyId': self.app_key,
    'Action': self.action,
    'Format': self.format,
    'PhoneNumbers': phone,
    'RegionId': self.region_id,
    'SignName': self.sign_name,
    'SignatureMethod': self.signature_method,
    'SignatureNonce': str(uuid.uuid4()),
    'SignatureVersion': self.signature_version,
    'TemplateCode': template_code,
    'TemplateParam': json.dumps(template_params),
    'Timestamp': datetime.datetime.utcnow().isoformat("T"),
    'Version': self.sms_version,
    }


    if __name__ == '__main__':
    app_key = 'your_app_key'
    app_secret = 'your_app_secret'
    sign_name = 'your_sign_name'
    template_code = 'your_template_code:SMS_123456789'
    template_params = {
    'code': '1024'
    }
    sms = AliDaYuSMS(app_key, app_secret, sign_name)
    resp = sms.send('phone_number', template_code, template_params)
    print(resp.status_code, resp.json())