C++ WebHook发送信息
2020-04-12
尼玛没什么好说的了,让我上代码,已经给大家封装为函数方便使用
#include <iostream>
#include <string>
#include <curl/curl.h>
using namespace std;
string GbkToUtf8(const char *src_str)
{
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
string strTemp = str;
if (wstr) delete[] wstr;
if (str) delete[] str;
return strTemp;
}
void sendtext(string content , string key)
{
string url = "https://app.qun.qq.com/cgi-bin/api/hookrobot_send?key=" + key;
string f = R"({"content": [ {"type":0,"data":")";
string l = R"("}]})";
string text = f + content + l;
////cout << text << endl;
const char * gbk = text.c_str();
string utf8 = GbkToUtf8(gbk);
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, url);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Postman-Token: 2909eca0-637f-4045-a524-c27a199ab04f");
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, utf8.c_str());
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE, utf8.size());
CURLcode ret = curl_easy_perform(hnd);
}
int main()
{
string content = "要发送的文本"; //要发送的文本
string key = "115e5898fe2bbc0873d995623f4c92b2d64e13ef";//记得把key换一下谢谢
sendtext(content, key);
while (true);
return 0;
}
发表评论: