1
0
forked from erp-dev/erp
Files
erpnew/api_v1/management/commands/wecom_webhook_send_text.py

42 lines
1.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.
from django.core.management.base import BaseCommand, CommandError
from api_v1.utils.wecom_webhook import send_wecom_webhook_message
class Command(BaseCommand):
help = "发送企业微信机器人 webhook 文本消息(用于测试)"
def add_arguments(self, parser):
parser.add_argument("text", type=str, help="要发送的文本内容")
parser.add_argument(
"--msgtype",
type=str,
choices=["text", "markdown"],
default="text",
help="消息类型(默认 text",
)
parser.add_argument("--key", type=str, default=None, help="可选:临时覆盖 settings.WECOM_WEBHOOK_KEY")
parser.add_argument(
"--timeout",
type=float,
default=10.0,
help="HTTP 超时(秒)",
)
def handle(self, *args, **options):
text = options["text"]
msgtype = options["msgtype"]
key = options["key"]
timeout = options["timeout"]
try:
resp = send_wecom_webhook_message(content=text, msgtype=msgtype, key=key, timeout_seconds=timeout)
except Exception as exc:
raise CommandError(str(exc)) from exc
# 企业微信成功时 errcode=0
if not resp.ok:
raise CommandError(f"WeCom webhook 返回失败errcode={resp.errcode}, errmsg={resp.errmsg}, raw={resp.raw}")
self.stdout.write(self.style.SUCCESS(f"OK: {resp.raw}"))