ABAP推送企业微信机器人5-文件类型

参数说明

1. 文件发送接口参数

参数是否必填说明
msgtype消息类型,此时固定为file
content文件id,通过下文的文件上传接口获取

2. 文件上传接口参数

素材上传得到media_id,该media_id仅三天内有效
media_id只能是对应上传文件的机器人可以使用

请求方式:POST(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=KEY&type=TYPE
上传的文件限制:要求文件大小在5B~20M之间
使用multipart/form-data POST上传文件, 文件标识名为”media”

参数是否必填说明
key调用接口凭证, 机器人webhookurl中的key参数
type固定传file

3. 返回参数

{
   "errcode": 0,
   "errmsg": "ok",
   "type": "file",
   "media_id": "1G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",
   "created_at": "1380000000"
}
参数是否必填说明
key媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
media_id媒体文件上传后获取的唯一标识,3天内有效
created_at媒体文件上传时间戳

效果展示

在这里插入图片描述

Demo Code

FUNCTION ZZF_HR_TX_ROBOT_POST_FILE.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IV_FIELD_NAME) TYPE  STRING
*"  EXPORTING
*"     VALUE(EV_MEDIA_ID) TYPE  STRING
*"  TABLES
*"      E_RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"      ET_TABLE
*"----------------------------------------------------------------------


*-----------------------------------------------------------------------
* create function for log
*-----------------------------------------------------------------------
  /afl/log_init.


*  DATA:LV_TX_ROBOT_WEBHOOK TYPE STRING VALUE 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=50d640d9-9ad6-4d39-98aa-c94c20021234'."随便写的号
  DATA:LV_ZIF_ID LIKE ZHRT_0001-ZIF_ID,
       LO_HTTP_CLIENT              TYPE REF TO IF_HTTP_CLIENT,
       LV_SERVICE                  TYPE STRING,
       LV_RESULT_STRING            TYPE STRING,
       LV_LEN                      TYPE I,
       LV_MSG                      TYPE STRING,
       LV_MESSAGE                  TYPE STRING,
       LV_HTTP_METHOD              TYPE STRING,
       LV_STRING                   TYPE STRING,
       LV_XSTRING                  TYPE XSTRING,
       LV_MEDIA_ID                 TYPE STRING,
       LV_HEAD_FIELD               TYPE STRING,
       LO_ENTITY                   TYPE REF TO IF_HTTP_ENTITY,
       LV_NAME                     TYPE STRING,
       LV_FILENAME                 TYPE STRING,
       LV_FIELD_SYMBOLS_STRING     TYPE STRING,
       LO_RESPONE_DATA             TYPE REF TO DATA,
       IV_URL                      TYPE STRING,
       PART                        TYPE REF TO IF_HTTP_ENTITY,
       LV_FIELNAME_LENGTH          TYPE I.


  DATA:LV_FIRST TYPE C.

  LV_ZIF_ID = 'HR00000032'.
  LV_HTTP_METHOD = IF_HTTP_REQUEST=>CO_REQUEST_METHOD_POST.

  SELECT SINGLE *
    INTO @DATA(LS_ZHRT_0001)
    FROM ZHRT_0001
   WHERE CLIENT EQ @SY-MANDT
     AND ZIF_ID EQ @LV_ZIF_ID.
  IF SY-SUBRC NE 0.
    E_RETURN-TYPE = 'E'.
    E_RETURN-MESSAGE = TEXT-E01."接口未配置
    RETURN.
  ENDIF.

  IF LS_ZHRT_0001-ZIF_OPEN EQ SPACE.
    E_RETURN-TYPE = 'E'.
    E_RETURN-MESSAGE = TEXT-E01."接口启用
    RETURN.
  ENDIF.

  CLEAR:LV_STRING.
  LOOP AT ET_TABLE ASSIGNING FIELD-SYMBOL(<FS_TABLE>).

    CLEAR:LV_FIRST.

    AT FIRST.
      LV_STRING = <FS_TABLE>.
*      CONCATENATE <FS_TABLE> LV_STRING INTO LV_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>NEWLINE.
      LV_FIRST = 'X'.
    ENDAT.

    IF LV_FIRST NE 'X'.
      CONCATENATE LV_STRING <FS_TABLE> INTO LV_STRING SEPARATED BY CL_ABAP_CHAR_UTILITIES=>NEWLINE.
    ENDIF.

  ENDLOOP.

  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
*     codepage_to      = '8300'
      MIMETYPE         = '"APPLICATION/MSEXCEL; charset=gb2312"'
      TEXT             = LV_STRING
    IMPORTING
      BUFFER           = LV_XSTRING
    EXCEPTIONS
      FAILED = 1
      OTHERS = 2.

   LV_SERVICE = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=' && LS_ZHRT_0001-ZPARA1_STR && '&type=file'.

   "clear:LO_HTTP_CLIENT.
   CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
      EXPORTING
         URL          = LV_SERVICE
      IMPORTING
         CLIENT        = LO_HTTP_CLIENT
      EXCEPTIONS
         ARGUMENT_NOT_FOUND = 1
         PLUGIN_NOT_ACTIVE = 2
         INTERNAL_ERROR = 3.

    CALL METHOD LO_HTTP_CLIENT->request->set_method( if_http_request=>co_request_method_post ).

    CALL METHOD LO_HTTP_CLIENT->request->set_version( if_http_request=>co_protocol_version_1_0 ).

    CALL METHOD LO_HTTP_CLIENT->request->if_http_entity~set_content_type(
      EXPORTING
        content_type = 'multipart/form-data' ).

    CALL METHOD LO_HTTP_CLIENT->request->if_http_entity~set_formfield_encoding
      EXPORTING
        formfield_encoding = cl_http_request=>if_http_entity~co_encoding_raw.

    part = LO_HTTP_CLIENT->request->if_http_entity~add_multipart( ).

*    LV_FILENAME = CL_HTTP_UTILITY=>UNESCAPE_URL( IV_FIELD_NAME ).
    LV_FILENAME = IV_FIELD_NAME.
    LV_FIELNAME_LENGTH = STRLEN( LV_FILENAME ).
*    LV_FILENAME = CL_HTTP_UTILITY=>ESCAPE_URL( IV_FIELD_NAME ).

    LV_HEAD_FIELD = 'form-data; name="media";filename="' && LV_FILENAME && '"; filelength=' && LV_FIELNAME_LENGTH .
    CALL METHOD part->set_header_field
      EXPORTING
        name  = 'Content-Disposition'
        value = LV_HEAD_FIELD
        .

    CALL METHOD part->set_content_type
      EXPORTING
        content_type = 'application/octet-stream'.
*        content_type = 'text/plain'.

    LV_LEN = XSTRLEN( LV_XSTRING ).

    CALL METHOD PART->SET_DATA
      EXPORTING
        DATA   = LV_XSTRING
        OFFSET = 0
        LENGTH = LV_LEN.


  CALL METHOD LO_HTTP_CLIENT->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.

  "读取远程服务返回的处理过结果。
  CALL METHOD LO_HTTP_CLIENT->RECEIVE
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2
      HTTP_PROCESSING_FAILED     = 3.
  IF SY-SUBRC <> 0 .
    LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = LV_MESSAGE ).
*    E_RETURN-MESSAGE = LV_MESSAGE.
*    E_RETURN-TYPE = 'E'.
    RETURN.
  ELSE.
    LV_RESULT_STRING = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
    LO_RESPONE_DATA = /UI2/CL_JSON=>GENERATE( JSON = LV_RESULT_STRING ).
    IF LO_RESPONE_DATA IS NOT INITIAL.
      LV_FIELD_SYMBOLS_STRING = 'LO_RESPONE_DATA->MEDIA_ID->*'.
      ASSIGN (LV_FIELD_SYMBOLS_STRING) TO FIELD-SYMBOL(<FS_MEDIA_ID>).

      IF <FS_MEDIA_ID> IS ASSIGNED.
        EV_MEDIA_ID = <FS_MEDIA_ID>.
        E_RETURN-TYPE = 'S'.
        E_RETURN-MESSAGE = TEXT-S04 && EV_MEDIA_ID .
        APPEND E_RETURN.
      ELSE.
        E_RETURN-TYPE = 'E'.
        E_RETURN-MESSAGE = LV_RESULT_STRING .
        APPEND E_RETURN.
      ENDIF.
    ELSE.
      E_RETURN-TYPE = 'E'.
      E_RETURN-MESSAGE = LV_RESULT_STRING .
      APPEND E_RETURN.
    ENDIF.
  ENDIF.


*-----------------------------------------------------------------------
* SAVE function log
*-----------------------------------------------------------------------
  /afl/save 'HR' 'TX_ROBOT_POST_FILE' '0'.



ENDFUNCTION.
posted @ 2021-09-23 17:54  linhuang  阅读(26)  评论(0编辑  收藏  举报  来源