调的是公司提供的接口
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; }
}
}