今天在做订单发送消息给用户邮件时,突然想起,其实SAP发消息给邮件,不如发给企业微信来的方便,企业微信用户能随时得到信息内容,使用更方法,所以有化了结时间研究了一下腾讯给的企业微信API,发现SAP是完成可以发送信息给企业微信,并且发送方法简单,直接HTTP发送GET,POST信息就行,我先使用POSTMAN测试了发送的内容,后面再写ABAP代码,
1.创建企业微信
在使用企业微信前,先得创建好企业微信,一般公司里已有企业微信了,所以以下信息可以让管理员从管理端获得。
1.得到企业ID,从地址中找到企业ID,此值在就是后面的corpid,比如我的企业ID为:ww5**************b77
2.得到Secret,AgentId,这两人值,需要先创建一个运用,并打开运行,如已有运用,放入在后台打开,如图就能找到这两个值了,其中Secret需要点击“查看”就能看到了。
2. 得到ACCESS_TOKEN
使用前面的corpid,corpsecret,使用GET方法,打开地址https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww******77&corpsecret=px9*****************************e7FpQ
返回的结果是一个JSON的结果,在其中的access_token值就是需要的值了,如下:
3.发送文本消息
发送消息可发送到多人,多部门等,更多细节可看这里,其中的touser,可使用后面的方法得到,发送时使用POST方法,发送地址为https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ygauT5******HobA,其中的access_token值就是前面一步得到的。POST发送的内容为支持简单的HTML格式,以下是我发送成功的一人报文内容,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "touser": "ken", "toparty": "", "totag": "", "msgtype": "text", "agentid": 1000002, "text": { "content": "SAP测试,你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。" }, "safe": 0, "enable_id_trans": 0, "enable_duplicate_check": 0 } |
通过以及操作后,在用户KEN的企业微信上“SAP消息”中就会收到了在的消息内容了。
4.得到用户ID
在前面的步骤中,使用的touser参数是企业微信里的用户ID,不是用户名,所以我们可以使用API接口来得到用户ID.使用GET方法查询以一地址就可以得
https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=2&fetch_child=1&access_token=7j7Lo******jnZVQ
其中的department_id是部门ID,可以使用,0,1,2,3测试查询,也是有一个API可以得到的一般企业应该不多,所以可以直都查一就成了。返回结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ "errcode": 0, "errmsg": "ok", "userlist": [ { "userid": "yangsen", "name": "yangsen", "department": [ 2 ] }, { "userid": "ken", "name": "Ken", "department": [ 2 ] } ] } |
5.ABAP实现
打包代码
测试时发现HTTPS时不能打开,应该是还需要STRUST安装证书,看这里。所以做了一个测试使用的例子,来查看SAP操作操作HTTP的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
*&********************************************************************* *& PROGRAM NAME : ZTEST20 *& Module Name : *& Apply Author : *& Author : *& Started on : 2020-07-01 *& Transaction : ZTEST20 *& Program type : Report *& Program ID : ZTEST20 *& Program Description : 功能描述。。。。。。 *&*&******************************************************************* *& REVISION LOG * *& * *& LOG# DATE AUTHOR DESCRIPTION * *& ---- ---- ------ ----------- * *& 0001 2020-07-01 XXX Initial Creation *&********************************************************************* REPORT ZTEST20. DATA: LV_URL TYPE STRING, HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT, RETURN_STR TYPE STRING, LV_START TYPE I, LV_END TYPE I, LV_PARAM TYPE STRING, LV_BODY TYPE STRING, LV_LEN TYPE I. DATA: L_PHONE(20), L_STR TYPE STRING, STR_IN TYPE STRING, STR_OUT TYPE STRING. DATA: WF_PROXY TYPE STRING, WF_PORT TYPE STRING. "HTTP STR_IN = '你猜'. CALL METHOD CL_HTTP_UTILITY=>ESCAPE_URL EXPORTING UNESCAPED = STR_IN RECEIVING ESCAPED = STR_OUT. CONCATENATE '/weather_mini?city=' STR_OUT INTO LV_PARAM. LV_PARAM = '/cgi-bin/gettoken?corpid=ww52****883b77&corpsecret=px9DiXK*****EPdCe7FpQ' . CALL METHOD CL_HTTP_CLIENT=>CREATE EXPORTING HOST = 'qyapi.weixin.qq.com' SERVICE = '80' SCHEME = '1' "http PROXY_HOST = WF_PROXY PROXY_SERVICE = WF_PORT IMPORTING CLIENT = HTTP_CLIENT. HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED . CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~request_method' VALUE = 'GET'. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~server_protocol' VALUE = 'HTTP/1.1'. "host后的url CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~request_uri' VALUE = LV_PARAM. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = 'Content-Type' VALUE = 'application/json'. * "set body * CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA * EXPORTING * DATA = LV_PARAM * OFFSET = 0 * LENGTH = LV_LEN. * "get the param of url----没用 * clear LV_PARAM. * CALL METHOD HTTP_CLIENT->REQUEST->GET_URI_PARAMETER * EXPORTING * NAME = 'city' * RECEIVING * VALUE = LV_PARAM. CALL METHOD HTTP_CLIENT->SEND EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2. CALL METHOD HTTP_CLIENT->RECEIVE EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2 HTTP_PROCESSING_FAILED = 3. * 获取返回的数据 RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ). IF RETURN_STR IS INITIAL. WRITE: /'用cl_http_client=>create方法:' ,/ 'faild'. ELSE. WRITE: /'用cl_http_client=>create方法:' ,/ RETURN_STR. ENDIF. CALL METHOD HTTP_CLIENT->CLOSE. CONCATENATE 'http://qyapi.weixin.qq.com/cgi-bin/gettoken' '?' 'corpid=ww52****3b77&corpsecret=px9DiXK-***PdCe7FpQ'INTO LV_URL. "URL填全部,带参数 CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL EXPORTING URL = LV_URL IMPORTING CLIENT = HTTP_CLIENT. HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED . CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~request_method' VALUE = 'GET'. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~server_protocol' VALUE = 'HTTP/1.1'. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = 'Content-Type' VALUE = 'application/json'. " text/html 看情况填 * "set body * CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA * EXPORTING * DATA = LV_PARAM * OFFSET = 0 * LENGTH = LV_LEN. CALL METHOD HTTP_CLIENT->SEND EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2. CALL METHOD HTTP_CLIENT->RECEIVE EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2 HTTP_PROCESSING_FAILED = 3. * 获取返回的数据 RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ). IF RETURN_STR IS INITIAL. WRITE: /'用CL_HTTP_CLIENT=>CREATE_BY_URL方法:' ,/ 'faild'. ELSE. WRITE: /'用CL_HTTP_CLIENT=>CREATE_BY_URL方法:' ,/ RETURN_STR. ENDIF. CALL METHOD HTTP_CLIENT->CLOSE. CONCATENATE 'https://qyapi.weixin.qq.com/cgi-bin/gettoken' '?' 'corpid=ww526***83b77&corpsecret=px9DiX***Ce7FpQ'INTO LV_URL. "URL填全部,带参数 CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL EXPORTING URL = LV_URL IMPORTING CLIENT = HTTP_CLIENT. HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED . CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~request_method' VALUE = 'GET'. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~server_protocol' VALUE = 'HTTP/1.1'. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = 'Content-Type' VALUE = 'application/json'. " text/html 看情况填 * "set body * CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA * EXPORTING * DATA = LV_PARAM * OFFSET = 0 * LENGTH = LV_LEN. CALL METHOD HTTP_CLIENT->SEND EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2. CALL METHOD HTTP_CLIENT->RECEIVE EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2 HTTP_PROCESSING_FAILED = 3. * 获取返回的数据 RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ). IF RETURN_STR IS INITIAL. WRITE: /'用CL_HTTP_CLIENT=>CREATE_BY_URL https方法:' ,/ 'faild'. ELSE. WRITE: /'用CL_HTTP_CLIENT=>CREATE_BY_URL https方法:' ,/ RETURN_STR. ENDIF. CALL METHOD HTTP_CLIENT->CLOSE. CALL METHOD CL_HTTP_CLIENT=>CREATE EXPORTING HOST = 'qyapi.weixin.qq.com' SERVICE = '443' SCHEME = '2' "https SSL_ID = 'ANONYM' PROXY_HOST = WF_PROXY PROXY_SERVICE = WF_PORT IMPORTING CLIENT = HTTP_CLIENT. HTTP_CLIENT->PROPERTYTYPE_LOGON_POPUP = HTTP_CLIENT->CO_ENABLED . CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~request_method' VALUE = 'GET'. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING * NAME = '~server_protocol' NAME = '~request_protocol' VALUE = 'HTTPS/1.1'. "host后的url CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~request_uri' VALUE = '/callback?cmd=1059&callback=phone&&phone=13205334302'. CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = 'Content-Type' * VALUE = 'application/x-www-form-urlencoded'. VALUE = 'application/json'. "host后的url CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD EXPORTING NAME = '~request_uri' VALUE = LV_PARAM. * "set body * CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA * EXPORTING * DATA = LV_BODY * OFFSET = 0 * LENGTH = LV_LEN. * "get the param of url----没用 * clear LV_PARAM. * CALL METHOD HTTP_CLIENT->REQUEST->GET_URI_PARAMETER * EXPORTING * NAME = 'city' * RECEIVING * VALUE = LV_PARAM. CALL METHOD HTTP_CLIENT->SEND EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2. CALL METHOD HTTP_CLIENT->RECEIVE EXCEPTIONS HTTP_COMMUNICATION_FAILURE = 1 HTTP_INVALID_STATE = 2 HTTP_PROCESSING_FAILED = 3. * 获取返回的数据 RETURN_STR = HTTP_CLIENT->RESPONSE->GET_CDATA( ). IF RETURN_STR IS INITIAL. WRITE: /'HTTPS:' ,/ 'faild'. ELSE. WRITE: /'HTTPS:' ,/ RETURN_STR. ENDIF. CALL METHOD HTTP_CLIENT->CLOSE. |
1.取得用户列表到自定义表
2.得到ACCESS_TOKEN
3.发送消息