钉钉 机器人接入 自定义webhook
钉钉出了个webhook机器人接入,自定义的机器人支持随时post消息到群里;
昨天就尝试着用C#写了个;
一开始用python写,但是莫名的提示 {"errmsg":"param error","errcode":300001} 错误,估计是某个参数不对,或者格式不争气;
顺带附上exe和工程源码,需要的同学可以下载;
代码烂莫喷哈~~
https://files.cnblogs.com/files/left69/机器人code.rar
打包好的 exe:
https://files.cnblogs.com/files/left69/机器人exe.rar
贴上C#的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.IO; using System.IO.Compression; using System.Text.RegularExpressions; namespace 机器人 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string paraUrlCoded = "{\"msgtype\":\"text\",\"text\":{\"content\":\""; paraUrlCoded += textBox2.Text; paraUrlCoded += "\"}}"; Post(paraUrlCoded); } private void button2_Click(object sender, EventArgs e) { string paraUrlCoded = "{\"msgtype\": \"link\", \"link\": {\"text\": \"我的博客:欢迎光临\", \"title\": \"推广博客啦,机器人开发者\", \"picUrl\": \"\", \"messageUrl\": \"http://www.cnblogs.com/left69/\"}}"; Post(paraUrlCoded); } private void Post(string paraUrlCoded) { string url = textBox1.Text; string strURL = url; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; byte[] payload; payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); request.ContentLength = payload.Length; Stream writer = request.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); string StrDate = ""; string strValue = ""; StreamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "\r\n"; } label3.Text = strValue; } } }
******************************************************************************************
后面贴一段 python的代码:大家可以研究下为啥post失败 (看后面,已改)
# -*- coding: cp936 -*-
import urllib2
import urllib
#定义一个要提交的数据数组(字典)
data = {
"msgtype": "text",
"text": {
"content": "我就是我来了"
}
}
#定义post的地址
url = 'https://oapi.dingtalk.com/robot/send?access_token=1cc4cb4cdda998ecc50d4b6f2a12cba311766743506d0ef251e09da3321ff776'
post_data = urllib.urlencode(data)
#提交,发送数据
req = urllib2.urlopen(url, post_data)
#获取提交后返回的信息
content = req.read()
print content
/************************************原因出来了,是没有加 head
/************************************正确
import requests
import json
import urllib.parse
url = 'https://oapi.dingtalk.com/robot/send?access_token=1cc4cb4cdda998ecc50d4b6f2a12cba311766743506d0ef251e09da3321ff776'
HEADERS = {
"Content-Type": "application/json ;charset=utf-8 "
}
String_textMsg = {\
"msgtype": "text",\
"text": {"content": '我就是我来了2'}}
String_textMsg = json.dumps(String_textMsg)
res = requests.post(url, data=String_textMsg, headers=HEADERS)
print(res.text)