Przeglądaj źródła

1、添加腾讯云短信推送.h及.cpp文件及调用测试文件。

liyuezong 2 lat temu
rodzic
commit
f11ab73f38

+ 1 - 0
Liyuezong/SNMP协议设计/Gowone-SmartPDU-mib-V2c/agent代理开发的.c及.h文件/main_demo.cpp

@@ -24,6 +24,7 @@ int main()
  
    /* initialize your mib code here */
    init_pduMIB();
+   
  
    /* pduMIB will be used to read pduMIB.conf files. */
    init_snmp("pduMIB");

+ 16 - 14
Liyuezong/短信推送设计/Message.cpp

@@ -1,13 +1,5 @@
-#include <iostream>
-#include <iomanip>
-#include <sstream>
-#include <string>
-#include <stdio.h>
-#include <time.h>
-#include <openssl/sha.h>
-#include <openssl/hmac.h>
+#include "Message.h"
 
-using namespace std;
 
 string get_data(int64_t &timestamp)
 {
@@ -95,11 +87,11 @@ string HexEncode(const string &input)
     return output;
 }
 
-int main()
+bool SendMessage(int nTemplateId)
 {
     // 密钥参数,云API密匙查询: https://console.cloud.tencent.com/cam/capi
-    string SECRET_ID = "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******";
-    string SECRET_KEY = "Gu5t9xGARNpq86cd98joQYCN3*******";
+    string SECRET_ID = "AKID1vnmgR1PgmktyqU2NVQ8ex61iWHrJfrw";
+    string SECRET_KEY = "zvhGcljT6nPq470fBSaWFvT5ZvLfdTQJ";
 
     string service = "sms";
     string host = "sms.tencentcloudapi.com";
@@ -116,7 +108,7 @@ int main()
     string canonicalHeaders = "content-type:application/json; charset=utf-8\nhost:" + host + "\n";
     string signedHeaders = "content-type;host";
     // 实际调用需要更新参数,这里仅作为演示签名验证通过的例子
-    string payload = "{\"SmsSdkAppId\":\"1400006666\",\"TemplateParamSet\":[\"12345\"],\"PhoneNumberSet\":[\"+8618511122266\"],\"SessionContext\":\"test\",\"SignName\":\"腾讯云\",\"TemplateId\":\"1234\"}";
+    string payload = "{\"SmsSdkAppId\":\"1400895227\",\"TemplateParamSet\":[\"4\",\"ping !\"],\"PhoneNumberSet\":[\"+8615982211342\"],\"SessionContext\":\"PDU设备{1}网络状态异常:{2},请及时关注设备网络状态!\",\"SignName\":\"成都购旺科技\",\"TemplateId\":\"2108490\"}";
     string hashedRequestPayload = sha256Hex(payload);
     string canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
             + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestPayload;
@@ -153,4 +145,14 @@ int main()
                    + " -H \"X-TC-Region: " + region + "\""
                    + " -d '" + payload + "'";
     cout << curlcmd << endl;
-    return 0;
+
+    //发送请求
+    int result = std::system(curlcmd.c_str());  
+    if (result == 0) {  
+        std::cout << "curl command succeeded." << std::endl;  
+		return true;
+    } else {  
+        std::cerr << "curl command failed with exit code: " << result << std::endl;  
+		return false;
+    }  
+}

+ 25 - 0
Liyuezong/短信推送设计/短信腾讯云.h头文件及调用示例/Message.h

@@ -0,0 +1,25 @@
+#ifndef MESSAGE_H  
+#define MESSAGE_H   
+  
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <string>
+#include <stdio.h>
+#include <time.h>
+#include <openssl/sha.h>
+#include <openssl/hmac.h>
+ 
+using namespace std; 
+   
+string get_data(int64_t &timestamp);
+string int2str(int64_t n);
+string sha256Hex(const string &str);
+string HmacSha256(const string &key, const string &input);
+string HexEncode(const string &input);
+
+// curl向腾讯云发送短信post请求  
+bool SendMessage(int nTemplateId);
+
+
+#endif // MESSAGE_H 

+ 17 - 0
Liyuezong/短信推送设计/短信腾讯云.h头文件及调用示例/SendMessageTest.cpp

@@ -0,0 +1,17 @@
+#include "Message.h"
+#include <string>
+ 
+int main(int argc, char* argv[]) 
+{
+  bool SendFlag=SendMessage(2108490);
+  if(SendFlag)
+  {
+	  printf("send Message successful! TemplateId:%d", 2108490);
+  }
+  else{
+	  printf("send Message failed! TemplateId:%d", 2108490);
+  }
+  
+  getchar();
+  return 0;
+}

+ 10 - 0
Liyuezong/短信推送设计/短信腾讯云.h头文件及调用示例/腾讯云API调用说明.txt

@@ -0,0 +1,10 @@
+1、腾讯云短信API推送需要curl和ssh实现加密post请求,需要安装相应的依赖环境:
+sudo apt-get install libcurl4-openssl-dev
+sudo apt-get install libssl-dev
+
+
+
+2、sendmessage函数里面的短信内容、签名、参数这些信息必须完全符合短信模版,且有长度要求,否则推送失败。
+具体看腾讯云短信签名方法V3具体要求:https://cloud.tencent.com/document/product/382/52072
+
+建议用nlohmann/json重新组织短信JSON内容,用string手写JSON不太直观。