釘釘出了個webhook機器人接入,自定義的機器人支持隨時post消息到群里;
昨天就嘗試着用C#寫了個;
一開始用python寫,但是莫名的提示 {"errmsg":"param error","errcode":300001} 錯誤,估計是某個參數不對,或者格式不爭氣;
順帶附上exe和工程源碼,需要的同學可以下載;
代碼爛莫噴哈~~
http://files.cnblogs.com/files/left69/機器人code.rar
打包好的 exe:
http://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)