1)在企业微信创建一个自建应用
2)设置自建应用的logo,名称等
其中AgentId相当于后面代码里面的agentid,Secret相当于后面代码里面的corpsecret,在可见范围可以设置谁可以收到推送的信息,这里也可以在代码里指定接收人
3)在我的企业,记下企业的ID:
4)打开vs,新建一个项目,选择类库,代码如下:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
using System.Net;
namespace vspostjson
{
public class Class1
{
public string GetAccessToken()
{
string _AccessToken = "";//存储微信访问凭证
DateTime _lastGetTimeOfAccessToken;//ACCESS_TOKEN最后一次更新时间
string corpId = "ww2…ce8";// 填入企业的corpId
string corpsecret = "BxZ23Zt…n1nLXU";// 填入Secret
string serviceAddress = string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", corpId, corpsecret);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
var rsEntity = new { access_token = "", expires_in = 0, errcode = 0, errmsg = "" };
dynamic en = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType<object>(retString, rsEntity); // Newtonsoft.Json提供的匿名类反序列化
_lastGetTimeOfAccessToken = DateTime.Now.AddSeconds((double)en.expires_in - 300);
return en.access_token;
}
public static string PostUrl(string msg,string access_token)
{
string touser = "@all";
string msgtype = "text";
string agentid = "1..2";//填入企业ID
string text = "{content: \""+msg+"\"}";
string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token;
string paramStr="{\"touser\": \"@all\",\"msgtype\": \"text\",\"agentid\": \"1000002\",\"text\": {\"content\": \""+msg+"\"},\"safe\": \"0\"}";//这里的touser就是选择接受者的ID,@all就是所有的用户
byte[] postData = Encoding.UTF8.GetBytes(paramStr);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("ContentLength", postData.Length.ToString());
byte[] responseData = client.UploadData(url, "POST", postData); //得到返回字符流 ;
return Encoding.UTF8.GetString(responseData);
}
}
}
5)生成dll文件。
6)编辑python插件:
import clr
clr.AddReference('Kingdee.BOS')
clr.AddReference('Kingdee.Bos.Core')
import sys
#生成的dll的路径
sys.path.append(r'C:\k3cloud')
#dll文件名
clr.AddReference('PostMsg6')
#从命名空间vspostjson 引入类Class
from vspostjson import Class1
from Kingdee.BOS.Core import *
def DataChanged(e):
if e.Key=="FQty" and e.NewValue<>"":
#实体化类
c=Class1()
#获取AccessToken
req = c.GetAccessToken()
#发送消息
sendmsg=c.PostUrl("123e",str(req))
this.View.ShowMessage(str(sendmsg))
7)运行结果:
再结合之前的python获取单据字段的值,就可以自定义要发送的内容
本文也顺便测试了k3cloud里面python引用dll文件。
推荐阅读