釘釘 機器人接入 自定義webhook

釘釘出了個webhook機器人接入,自定義的機器人支持隨時post消息到羣裏;python

昨天就嘗試着用C#寫了個;web

 一開始用python寫,可是莫名的提示  {"errmsg":"param error","errcode":300001}   錯誤,估計是某個參數不對,或者格式不爭氣;json

 

順帶附上exe和工程源碼,須要的同窗能夠下載;  api

 代碼爛莫噴哈~~數組

 http://files.cnblogs.com/files/left69/機器人code.rarapp

 

打包好的 exe:post

http://files.cnblogs.com/files/left69/機器人exe.rarurl

 

貼上C#的代碼spa

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;
        }
    }
}

 

 

******************************************************************************************code

後面貼一段 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 requestsimport jsonimport urllib.parseurl = '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)

相關文章
相關標籤/搜索