Skip to content

Instantly share code, notes, and snippets.

@khahux
Created August 19, 2019 05:08
Show Gist options
  • Save khahux/d4082a0ebb13672968025a47a711d9f3 to your computer and use it in GitHub Desktop.
Save khahux/d4082a0ebb13672968025a47a711d9f3 to your computer and use it in GitHub Desktop.
import time
import json
import hashlib
from random import randint
import requests
APP_ID = ''
APP_KEY = ''
URL = 'https://cloud.tim.qq.com/v5/tlsvoicesvr/sendvoiceprompt?sdkappid={0}&random={1}'
unicode_type = str
def utf8(value):
if isinstance(value, (bytes, type(None))):
return value
if not isinstance(value, unicode_type):
raise TypeError(
"Expected bytes, unicode, or None; got %r" % type(value)
)
return value.encode("utf-8")
def get_random():
return randint(100000, 999999)
def get_current_time():
return int(time.time())
def calculate_signature(rand, curtime, phone_numbers=None):
raw_text = "appkey={}&random={}&time={}".format(APP_KEY, rand, curtime)
if phone_numbers:
raw_text += "&mobile={}".format(
",".join(map(str, phone_numbers)))
return hashlib.sha256(utf8(raw_text)).hexdigest()
def main():
rand = get_random()
curtime = get_current_time()
phone_numbers = ['18310096020']
url = URL.format(APP_ID, rand)
sig = calculate_signature(rand, curtime, phone_numbers)
request_data = {
'ext': '',
'playtimes': 3,
'promptfile': '您好,今天北京天气不错,一会就下雨了。',
'prompttype': 2,
'sig': sig,
'tel': {
'mobile': phone_numbers[0],
'nationcode': '86'
},
'time': curtime
}
# response = requests.post(url, json=json.dumps(request_data), headers={"Content-Type": "application/json"})
response = requests.post(url, json=request_data)
print(response.json())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment