- 最近接到一个SAP直接给企业微信推送消息的需求,说实话之前一直没接触过,脑袋空空的,最终通过在百度搜索案例成功解决了,百度虽然一直被诟病,但却无法否认它的神奇。
- 实现效果
- 实现思路:从需求出发,SAP给企业微信推送消息,其本质上是SAP调用企业微信对外发布的API,按照这个思路去找企业微信的API就好了。
- 1.获取企业内部开发>服务端API>开发指南>获取access_tokenhttps://developer.work.weixin.qq.com/document/path/910392.企业内部开发>服务端API>消息推送>发送应用消息https://developer.work.weixin.qq.com/document/path/90236
- 开发技巧
- 获取access_token请求地址: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=企业ID&corpsecret=SECRET;两个参数企业ID和SECRET可以去找企业微信管理员的同事获取,拿到两个参数可以在postman测试;
- 根据第一步获取的ACCESS_TOKEN,再次调用企业微信API发送应用消息;请求地址: https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN
- ABAP 代码
DATA lv_get_url TYPE string. DATA lv_post_url TYPE string. TYPES:BEGIN OF ty_get_receive, errcode TYPE i, errmsg TYPE string, access_token TYPE string, expires_in TYPE i. TYPES:END OF ty_get_receive. DATA ls_get_receive TYPE ty_get_receive. TYPES:BEGIN OF ty_content, content TYPE string. TYPES:END OF ty_content. TYPES:BEGIN OF ty_post_request, touser TYPE string, msgtype TYPE string, agentid TYPE i, text TYPE ty_content, safe TYPE i, enable_id_trans TYPE i, enable_duplicate_check TYPE i, duplicate_check_interval TYPE i. TYPES:END OF ty_post_request. DATA ls_post_request TYPE ty_post_request. TYPES:BEGIN OF ty_post_receive, errcode TYPE i, errmsg TYPE string, msgid TYPE string. TYPES:END OF ty_post_receive. DATA ls_post_receive TYPE ty_post_receive. START-OF-SELECTION. lv_get_url = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?` && `corpid=企业ID` && `&corpsecret=企业应用Secret`. ls_post_request-text-content = |外购产品同步销售订单通知:\n| && |{ sy-datum DATE = ISO } { sy-uzeit TIME = ISO }\n| && |1234567890| && |采购订单没有同步销售订单,请知悉! |. "设置传输内容 ls_post_request-touser = `8073|404|613|646`. ls_post_request-msgtype = `text`. ls_post_request-agentid = 1000061. ls_post_request-safe = 0. ls_post_request-enable_id_trans = 0. ls_post_request-enable_duplicate_check = 0. ls_post_request-duplicate_check_interval = 0. *** 第一步:获取access_token请求 cl_http_client=>create_by_url( EXPORTING url = lv_get_url IMPORTING client = DATA(io_http_client) ). io_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ). io_http_client->request->set_method( if_http_request=>co_request_method_get ). io_http_client->send( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 ). IF sy-subrc NE 0. io_http_client->get_last_error( IMPORTING message = DATA(lv_message) code = DATA(lv_code) ). CONCATENATE '获取access_token失败原因:' lv_message INTO lv_message. ENDIF. io_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ). IF sy-subrc EQ 0. DATA(lv_get_res) = io_http_client->response->get_cdata( ). ELSE. io_http_client->get_last_error( IMPORTING message = DATA(lv_msg) code = DATA(lv_code1) ). CONCATENATE '企业微信服务器响应失败原因:' lv_msg INTO lv_msg. ENDIF. "将Json报文转换成SAP结构 /ui2/cl_json=>deserialize( EXPORTING json = lv_get_res pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_get_receive ). IF ls_get_receive-errmsg NE `ok`. MESSAGE '获取access_token失败' TYPE 'E'. ENDIF. *** 第二步:发送到企业微信 CLEAR:io_http_client. lv_post_url = `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=` && ls_get_receive-access_token. cl_http_client=>create_by_url( EXPORTING url = lv_post_url IMPORTING client = io_http_client ). io_http_client->request->set_content_type( content_type = 'application/json; charset=utf-8' ). io_http_client->request->set_method( if_http_request=>co_request_method_post ). DATA(lv_send_data) = /ui2/cl_json=>serialize( data = ls_post_request compress = abap_false pretty_name = /ui2/cl_json=>pretty_mode-camel_case ). io_http_client->request->set_cdata( data = lv_send_data ). io_http_client->send( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 ). IF sy-subrc NE 0. io_http_client->get_last_error( IMPORTING message = lv_message code = lv_code ). CONCATENATE '发送POST请求给企业微信失败:' lv_message INTO lv_message. ENDIF. io_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ). IF sy-subrc EQ 0. DATA(lv_post_res) = io_http_client->response->get_cdata( ). ELSE. io_http_client->get_last_error( IMPORTING message = lv_msg code = lv_code1 ). CONCATENATE '企业微信服务器响应失败原因:' lv_msg INTO lv_msg. ENDIF. "将Json报文转换成SAP结构 /ui2/cl_json=>deserialize( EXPORTING json = lv_post_res pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_post_receive ). IF ls_post_receive-errmsg NE `ok`. MESSAGE '发送POST请求给企业微信失败' TYPE 'S' DISPLAY LIKE 'E'. ELSE. MESSAGE '发送POST请求给企业微信成功' TYPE 'S'. ENDIF.
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » ABAP - SAP与企业微信集成
发表评论 取消回复