企业微信设置机器人通过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;
        }

    }
}
相关推荐
bin915318 分钟前
AI工具赋能Python开发者:项目开发中的创意守护与效率革命
开发语言·人工智能·python·工具·ai工具
被放养的研究生21 分钟前
Python常用的一些语句
开发语言·python
唐青枫27 分钟前
C#.NET FluentSqlKata 全面解析:基于链式语法的动态 SQL 构建
c#·.net
fox_lht2 小时前
第一章 不可变的变量
开发语言·后端·rust
骁的小小站3 小时前
Verilator 和 GTKwave联合仿真
开发语言·c++·经验分享·笔记·学习·fpga开发
心灵宝贝5 小时前
申威架构ky10安装php-7.2.10.rpm详细步骤(国产麒麟系统64位)
开发语言·php
lly2024065 小时前
PHP 字符串操作详解
开发语言
像是套了虚弱散7 小时前
DevEco Studio与Web联合开发:打造鸿蒙混合应用的全景指南
开发语言·前端·华为·harmonyos·鸿蒙
旭意7 小时前
C++蓝桥杯之结构体10.15
开发语言·c++
麦麦鸡腿堡8 小时前
Java的单例设计模式-饿汉式
java·开发语言·设计模式