| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- namespace molilian.core
- {
- public partial class NotifyCore
- {
- static string _base_url = string.Empty;
- static string _anpush_url = string.Empty;
- static string _qyweixin_push_url = string.Empty;
- static NotifyCore()
- {
- _anpush_url = Environment.GetEnvironmentVariable("ANPush");
- _qyweixin_push_url = Environment.GetEnvironmentVariable("QYWeixinPush");
- string? publish_url = Environment.GetEnvironmentVariable("NtfyServer");
- if (!string.IsNullOrEmpty(publish_url))
- {
- _base_url = publish_url;
- }
- }
- // 同步版本保持不变
- public static void Notify(string message)
- {
- if (!string.IsNullOrEmpty(_base_url))
- {
- NifyPlus.Send(_base_url, new NifyMessage { message = message });
- }
- }
- // 新增异步版本
- public static async Task NotifyAsync(string message)
- {
- if (!string.IsNullOrEmpty(_base_url))
- {
- await NifyPlus.SendAsync(_base_url, new NifyMessage { message = message });
- }
- }
- // 同步版本保持不变
- public static void Notify(NifyMessage message)
- {
- if (!string.IsNullOrEmpty(_base_url))
- {
- NifyPlus.Send(_base_url, message);
- }
- }
- // 新增异步版本
- public static async Task NotifyAsync(NifyMessage message)
- {
- if (!string.IsNullOrEmpty(_base_url))
- {
- await NifyPlus.SendAsync(_base_url, message);
- }
- }
- public static async Task QYWeixinPushNotifyAsync(string title, string content)
- {
- #if DEBUG
- await Task.CompletedTask;
- #else
- if (!string.IsNullOrEmpty(_qyweixin_push_url))
- {
- var args = new
- {
- msgtype = "markdown",
- markdown = new
- {
- content = $"# {title}\n{content}",
- }
- };
- string body = string.Empty;
- try
- {
- WebClientUtility client = new WebClientUtility();
- client.Post(args.Convert2Json());
- client.SetContentType("application/json");
- var response = await client.RequestAsync(_qyweixin_push_url, "POST");
- body = response.Body();
- new LoggerLibrary("qyweixin").Info(title, content).Info(body).Save();
- }
- catch (Exception ex)
- {
- new LoggerLibrary("qyweixin", "error")
- .Info(title, content)
- .Info(body)
- .Info(ex.Message, ex.StackTrace)
- .Save();
- }
- }
- #endif
- }
- // 同步版本保持不变
- public static void AnPushNotify(string title, string content)
- {
- #if DEBUG
- #else
- if (!string.IsNullOrEmpty(_anpush_url))
- {
- string url = _anpush_url;
- url = url.Replace("{title}", title?.UrlEncode());
- url = url.Replace("{content}", content?.UrlEncode());
- try
- {
- url.HttpRequest();
- }
- catch (Exception ex) { }
- }
- #endif
- }
- // 新增异步版本
- public static async Task AnPushNotifyAsync(string title, string content)
- {
- #if DEBUG
- await Task.CompletedTask;
- #else
- if (!string.IsNullOrEmpty(_anpush_url))
- {
- string url = _anpush_url;
- url = url.Replace("{title}", title?.UrlEncode());
- url = url.Replace("{content}", content?.UrlEncode());
- try
- {
- await Task.Run(() => url.HttpRequest());
- }
- catch (Exception ex) { }
- }
- #endif
- }
- }
- }
|