企业微信设置机器人通过winform提醒WxWorkBOT

配置文件

csharp 复制代码
        private static string url = ConfigurationManager.AppSettings["WxWorkBOTUrl"].ToString().Trim();

启动发送

csharp 复制代码
     /// <summary>
        /// 初始加载 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainFrm_Load(object sender, EventArgs e)
        {
            InitMethod();
            projectName = BasicDAL.GetProject();
            this.Text = $"【{projectName}】发票自动识别程序--{DbHelperSQL.connectionString}";

            WxWorkBOT.SendMess("Hi,我是**电子发票服务机器人 ,提醒:**电子发票服务已启动!");
        }

异常退出

csharp 复制代码
 /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += Application_ThreadException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ApplicationExit += Application_ApplicationExit;
            Application.Run(new MainFrm());
        }

        private static void Application_ApplicationExit(object sender, EventArgs e)
        {
            WxWorkBOT.SendMess("Hi,我是**电子发票服务机器人 ,提醒:**电子发票服务已断开!!!");

        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                WxWorkBOT.SendMess("Hi,我是**电子发票服务机器人 ,提醒:**合电子发票服务已断开!!!");

            }
            catch { }
        }
        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Process[] ps = Process.GetProcessesByName("AutoInvoiceFXLH");
            if (ps.Length < 1)
            {
                WxWorkBOT.SendMess("Hi,我是**电子发票服务机器人 ,提醒:**电子发票服务已断开!!!");

            }

        }
    }
csharp 复制代码
using Newtonsoft.Json;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;


namespace Common
{
    public class WxWorkBOT
    {
        private static string url = ConfigurationManager.AppSettings["WxWorkBOTUrl"].ToString().Trim();
              
        public static void SendMess(string mse,string connUrl = null)
        {
            try
            {
                var jsonObject = new { msgtype = "text", text = new { content = mse } };
                PostUrl((connUrl == null? url: connUrl), JsonConvert.SerializeObject(jsonObject));
            }
            catch (System.Exception)
            {

            }
        }

        /// <summary>
        /// 普通的post
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public static string PostUrl(string url, string postData)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            byte[] data = Encoding.UTF8.GetBytes(postData);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

    }
}
相关推荐
R_.L4 分钟前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
喵叔哟11 分钟前
06-ASPNETCore-WebAPI开发
服务器·后端·c#
Zach_yuan13 分钟前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
云姜.18 分钟前
java多态
java·开发语言·c++
CoderCodingNo27 分钟前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
2501_9307077830 分钟前
使用 C# .NET 从 PowerPoint 演示文稿中提取背景图片
c#·powerpoint·.net
陳103034 分钟前
C++:红黑树
开发语言·c++
一切尽在,你来39 分钟前
C++ 零基础教程 - 第 6 讲 常用运算符教程
开发语言·c++
泉-java41 分钟前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言
weixin_499771551 小时前
C++中的组合模式
开发语言·c++·算法