参数说明

参数

是否必填

说明

msgtype

消息类型,此时固定为text

content

文本内容,最长不超过2048个字节,必须是utf8编码

mentioned_list

userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list

mentioned_mobile_list

手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人

效果展示

ABAP推送企业微信机器人1-文本类型_远程服务

Demo Code

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_MEDIA_ID TYPE STRING,
LV_JSON_DATA TYPE STRING,
LS_RETURN TYPE BAPIRETURN.

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.
LS_RETURN-TYPE = 'E'.
LS_RETURN-MESSAGE = TEXT-E01."接口未配置
RETURN.
ENDIF.

IF LS_ZHRT_0001-ZIF_OPEN EQ SPACE.
LS_RETURN-TYPE = 'E'.
LS_RETURN-MESSAGE = TEXT-E01."接口启用
RETURN.
ENDIF.
* IF ET_TABLE[] IS NOT INITIAL.
*
* CALL FUNCTION 'ZZF_HR_TX_ROBOT_POST_FILE'
* EXPORTING
* IV_FIELD_NAME = IV_FIELD_NAME
* IMPORTING
* EV_MEDIA_ID = LV_MEDIA_ID
* TABLES
* ET_TABLE = ET_TABLE
* .
*
* ENDIF.

LV_SERVICE = LS_ZHRT_0001-ZURL && LS_ZHRT_0001-ZPARA1 .
REPLACE 'HR_PARA1' IN LV_SERVICE WITH LS_ZHRT_0001-ZPARA1_STR.


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
OTHERS = 4 ).

LO_HTTP_CLIENT->request->set_method( LV_HTTP_METHOD ).

* "设置字符类别
CALL METHOD LO_HTTP_CLIENT->request->set_header_field
EXPORTING
name = 'content-type'
value = 'application/json'.
LV_MSG =

'{' &&
'"msgtype": "text",' &&
'"text": {' &&
'"content": "Joker test for wechat Message!",' &&
'"mentioned_list":["linhuang","@all"],' && "userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list
'"mentioned_mobile_list":["13876543210","@all"]' && "手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
'}'.
LV_LEN = STRLEN( LV_MSG ).
CALL METHOD LO_HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = LV_MSG
OFFSET = 0
LENGTH = LV_LEN.

"设置待传输内容
CALL METHOD LO_HTTP_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2.
IF SY-SUBRC <> 0.
"操作失败,获取失败原因
LO_HTTP_CLIENT->GET_LAST_ERROR( IMPORTING MESSAGE = LV_MESSAGE ).
LS_RETURN-MESSAGE = LV_MESSAGE.
LS_RETURN-TYPE = 'E'.
RETURN..
ENDIF.

"读取远程服务返回的处理过结果。
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 ).
LS_RETURN-MESSAGE = LV_MESSAGE.
LS_RETURN-TYPE = 'E'.
RETURN.
ELSE.
LV_RESULT_STRING = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
CALL FUNCTION 'ZHTTP_UNICODE_TO_ZH'
EXPORTING
IV_STRING = LV_RESULT_STRING
IV_FLAG = '\u'
IMPORTING
RV_STRING = LV_RESULT_STRING.
LV_JSON_DATA = LV_RESULT_STRING.

ENDIF.

总结

简单快捷,可以直接@对应的人,mentioned_list和mentioned_mobile_list是个数组,多人的话直接往里面加入即可