url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/receive?key=" + token
看起来cgi-bin/webhook/receive像是个机器人的接口 ,但是文档上没有 是弃用了嘛 如果还再用 有相关文档吗?
下边贴出chatgpt 给出的代码示例:
/**
监听消息的方法*/
private static void ListenMessage(string accessToken, string token, int agentId)
{
// 循环请求接口
while (true)
{
string url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/receive?key=" + token;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + accessToken);
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string payload = "{"msgtype": "text", "agentid": "" + agentId.ToString() +
"","text":{"content":"Hello, World!"}}";
streamWriter.Write(payload);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
reader.Close();
response.Close();
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
// 请求间隔时间 (建议在>=1秒内)
Thread.Sleep(1000);
}
}