Files
vf_react/sms_sender.py
2025-12-12 00:42:55 +08:00

74 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
独立的短信发送脚本(绕过 eventlet DNS 问题)
使用方式:
python sms_sender.py <phone> <code> <template_id> <secret_id> <secret_key> <app_id> <sign_name>
返回值:
成功返回 0失败返回 1
"""
import sys
import json
def send_sms(phone, code, template_id, secret_id, secret_key, app_id, sign_name):
"""发送短信验证码"""
try:
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.sms.v20210111 import sms_client, models
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
cred = credential.Credential(secret_id, secret_key)
httpProfile = HttpProfile()
httpProfile.endpoint = "sms.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = sms_client.SmsClient(cred, "ap-beijing", clientProfile)
req = models.SendSmsRequest()
# 登录模板需要两个参数:验证码和有效期
template_params = [code, "5"] if "login" in template_id.lower() or template_id == "2365498" else [code]
params = {
"PhoneNumberSet": [phone],
"SmsSdkAppId": app_id,
"TemplateId": template_id,
"SignName": sign_name,
"TemplateParamSet": template_params
}
req.from_json_string(json.dumps(params))
resp = client.SendSms(req)
print(f"SMS sent successfully: {resp.to_json_string()}")
return True
except TencentCloudSDKException as err:
print(f"SMS Error: {err}")
return False
except Exception as e:
print(f"Unexpected error: {e}")
return False
if __name__ == "__main__":
if len(sys.argv) != 8:
print("Usage: python sms_sender.py <phone> <code> <template_id> <secret_id> <secret_key> <app_id> <sign_name>")
sys.exit(1)
phone = sys.argv[1]
code = sys.argv[2]
template_id = sys.argv[3]
secret_id = sys.argv[4]
secret_key = sys.argv[5]
app_id = sys.argv[6]
sign_name = sys.argv[7]
success = send_sms(phone, code, template_id, secret_id, secret_key, app_id, sign_name)
sys.exit(0 if success else 1)