企业微信群聊添加机器人(企业微信群聊机器人定时发送)(1)

工作需要 自己变成不在行 也是研究了很久 不敢独享,给大家分享下,讨论学习下

以下是部分代码 仅能实现发送信息功能

需要引用 Newtonsoft.Json.dll 需要的私信我 或根据VS提示进行下载引用

具体实现的代码

private bool send_Bots()

{

var obj = new

{

//msgtype = "markdown",//消息类型

//markdown = new

//{

// content = "实时新增用户反馈<font color=\"warning\">132例</font>,请相关注意。\n "

// "> 类型:< font color =\"comment\">用户反馈</font>\n"

// "> 普通用户反馈:< font color =\"comment\">117例</font>\n "

// "> VIP用户反馈:< font color =\"comment\">15例</font>"

//}

msgtype = "text",

text = new

{

content = txtStr.Text,

//mentioned_list =str,

//mentioned_mobile_list = str

}

};

string result = HttpHelper.PostUrlString(

httpUrl: "https://qyapi.weixin.qq.com/cgi-bin/webh",//这里是你的企业微信群聊机器人测试地址

//httpUrl: txtApi.Text,//测试地址可以像图片一样在text输入然后赋值

ContentType: "application/json",//请求数据类型

dataStr: JsonConvert.SerializeObject(obj),//请求数据

isProxy: false);//开启代理

return true;

}

需要新建类HttpHelper

private const string default_userAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36";

//默认请求类型

private const string default_ContentType = "application/x-www-form-urlencoded";

/// <summary>

/// 获取Post提交数据后返回的字符串

/// </summary>

/// <param name="httpUrl">Url地址</param>

/// <param name="dataStr">Post数据</param>

/// <param name="timeOut">请求响应超时时间,单位秒,默认30s</param>

/// <param name="Encoding">编码方式</param>

/// <param name="userAgent">HTTP User-agent值</param>

/// <param name="cookies">发送Cookie信息,常用于身份验证</param>

/// <returns></returns>

public static string PostUrlString(string httpUrl, string dataStr, int timeOut = 30, Encoding encoding = null, string ContentType = default_ContentType, string userAgent = default_userAgent, CookieCollection cookies = null, bool isProxy = false)

{

string result = "";

try

{

encoding = encoding ?? Encoding.UTF8;

HttpWebrequest Request = WebRequest.Create(httpUrl) as HttpWebRequest;

request.Method = "POST";

request.ContentType = ContentType;

request.UserAgent = userAgent;

request.Timeout = timeOut * 1000;

//发送Cookie信息

if (cookies != null)

{

request.CookieContainer = new CookieContainer();

request.CookieContainer.Add(cookies);

}

//设置代理

//if (isProxy)

//{

// WebProxy proxyObject = new WebProxy("ip", 端口);

// proxyObject.Credentials = new NetworkCredential("代理账号", "代理密码");

// request.Proxy = proxyObject;

//}

//发送提交内容

byte[] data = encoding.GetBytes(dataStr);

using (Stream stream = request.GetRequestStream())

{

stream.Write(data, 0, data.Length);

}

//获取响应内容

using (WebResponse wr = request.GetResponse())

{

//在这里对接收到的页面内容进行处理

using (StreamReader reader = new StreamReader(wr.GetResponseStream(), encoding))

{

result = reader.ReadToEnd();

}

}

}

catch (Exception )

{

//日志记录

}

return result;

}

,