• 最近接到一个SAP直接给企业微信推送消息的需求,说实话之前一直没接触过,脑袋空空的,最终通过在百度搜索案例成功解决了,百度虽然一直被诟病,但却无法否认它的神奇。
  • 实现效果

  1. 获取access_token请求地址: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=企业ID&corpsecret=SECRET;两个参数企业ID和SECRET可以去找企业微信管理员的同事获取,拿到两个参数可以在postman测试;
  2. 根据第一步获取的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.

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部