发送企微群消息和消息早知道

调的是公司提供的接口

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
//using Newtonsoft.Json;
using System.Text;
using System.Text.Json;
using System.Web;

namespace WeCharGroupTest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WechatGroupController : ControllerBase
    {

        /// <summary>
        /// 发送企微群组消息
        /// </summary>
        /// <param name="ChatId">群Id</param>
        /// <param name="Content">消息正文</param>
        /// <returns></returns>
        [HttpPost("Send_WechatGroup")]
        public async Task<string> Send_WechatGroup(string ChatId, string Content)
        {
            //公司提供的
            string encodedContent = HttpUtility.UrlEncode(Content);//发送的正文
            string url = "http://xxx:8000/lzwxapi/api/WorkWeChat/GroupTextMessage";//公司中央提供的接口
            string Code = "CN3G_zabbix";
            string Password = "Y7ftCrnmlI5N4y4G";
            int SendApp = 1;//1表示未发送,0表示已发送
            string postData = $"Code={Code}&Password={Password}&SendApp={SendApp}&ChatId={ChatId}&Content={encodedContent}";

            //调用接口
            using HttpClient httpClient = new HttpClient();
            StringContent stringContent = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");

            HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(url, stringContent);//调用接口
            string Msg = await httpResponseMessage.Content.ReadAsStringAsync();//返回值

            return Msg;
        }

        /// <summary>
        /// 企微消息早知道
        /// </summary>
        /// <param name="empCodes">工号</param>
        /// <param name="Content">消息正文</param>
        /// <returns></returns>
        [HttpPost("Send_Wechat")]
        public async Task<string> Send_Wechat(string empCodes, string Content)
        {
            //公司提供的
            string encodedContent = HttpUtility.UrlEncode(Content);//发送的正文
            string url = "http://xxx:8000/lzwxapi/api/WorkWeChat/SendTextMessage";//公司中央提供的接口
            string account = "CN3G_zabbix";
            string password = "Y7ftCrnmlI5N4y4G";
            int sendApp = 1;//1表示未发送,0表示已发送
            string postData = $"Account={account}&Password={password}&SendApp={sendApp}&EmpCodes={empCodes}&Content={encodedContent}";//ChatId:企微群唯一ID

            //调用接口
            using HttpClient cttpClient = new HttpClient();
            StringContent stringContent = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");

            HttpResponseMessage httpResponseMessage = await cttpClient.PostAsync(url, stringContent);
            string Msg = await httpResponseMessage.Content.ReadAsStringAsync();//返回消息值

            WeChatResult? weChatResult = JsonSerializer.Deserialize<WeChatResult>(Msg);
            return JsonSerializer.Serialize(weChatResult);
        }


    }

    public class WeChatResult
    {
        public bool IsSuccess { get; set; }
        public string? ErrMsg { get; set; }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值