参数:
IV_ID Importing Type STRING ID
IV_SECRET Importing Type STRING Secret
IV_USRID Importing Type STRING 接受者
IV_AGENTID Importing Type STRING 应用ID
IV_CONTENTTEXT Importing Type STRING 内容
ES_RETURN Exporting Type BAPI_CORU_RETURN
代码:
" Token
TYPES:
BEGIN OF ty_token,
errcode TYPE string,
errmsg TYPE string,
access_token TYPE string,
expires_in TYPE string,
END OF ty_token.
" 消息
TYPES: BEGIN OF ty_text,
content TYPE string,
END OF ty_text.
TYPES: BEGIN OF ty_send_body,
touser TYPE string,
toparty TYPE string,
totag TYPE string,
msgtype TYPE string,
agentid TYPE string,
text TYPE ty_text,
safe TYPE string,
enable_id_trans TYPE string,
enable_duplicate_check TYPE string,
duplicate_check_interval TYPE string,
END OF ty_send_body.
" 接口返回
TYPES:
BEGIN OF ty_return,
errcode TYPE string, "错误码
errmsg TYPE string, "错误消息
userid TYPE string, "用户ID
invaliduser TYPE string,
invalidparty TYPE string,
msgid TYPE string,
response_code TYPE string,
END OF ty_return.
DATA:lv_url TYPE string, " 接口地址
lv_cdata TYPE string, " 请求体
lv_ret_result TYPE string, " 返回字符串
ls_return TYPE ty_return, " 消息返回结构
ls_send_body TYPE ty_send_body, " 请求体结构
ls_token TYPE ty_token. " Token 结构
DATA: ls_message TYPE bapi_conf_return.
" Token 地址
CONCATENATE 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
'?corpid='
iv_id
'&corpsecret=' "密钥
iv_secret
INTO lv_url.
CONDENSE lv_url NO-GAPS.
" 获取token
me->send_request(
EXPORTING
iv_url = lv_url
IMPORTING
ev_result = lv_ret_result
es_return = ls_message
).
IF ls_message-type <> 'E'.
" Token
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = lv_ret_result
" assoc_arrays = assoc_arrays
CHANGING
data = ls_token.
CLEAR: lv_url,lv_cdata,lv_ret_result,ls_message.
" 企微消息发送
CONCATENATE 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' "token
ls_token-access_token
INTO lv_url.
CONDENSE lv_url NO-GAPS.
ls_send_body-touser = iv_usrid.
ls_send_body-msgtype = 'text'.
ls_send_body-agentid = iv_agentid.
ls_send_body-text-content = iv_contenttext.
ls_send_body-safe = '0'.
ls_send_body-enable_id_trans = '0'.
ls_send_body-enable_duplicate_check = '1'.
ls_send_body-duplicate_check_interval = '1800'.
lv_cdata = /ui2/cl_json=>serialize(
data = ls_send_body
pretty_name = /ui2/cl_json=>pretty_mode-low_case
).
" abap2json时换行符\n会转换为\\\\n,官方api中换行为\n
REPLACE ALL OCCURRENCES OF '\\n' IN lv_cdata WITH '\n'.
me->send_request(
EXPORTING
iv_cdata = lv_cdata
iv_url = lv_url
IMPORTING
ev_result = lv_ret_result
es_return = ls_message
).
IF ls_message-type <> 'E'.
CALL METHOD /ui2/cl_json=>deserialize
EXPORTING
json = lv_ret_result
CHANGING
data = ls_return.
IF ls_return-errcode = 0.
es_return-type = 'S'.
ELSE.
es_return-type = 'E'.
ENDIF.
es_return-message = ls_return-errmsg.
ELSE.
es_return = ls_message.
ENDIF.
ELSE.
es_return = ls_message.
ENDIF.
调用方法send_request
参数:
IV_CDATA Importing Type STRING Json data
IV_URL Importing Type STRING url
EV_RESULT Exporting Type STRING Return Result
ES_RETURN Exporting Type BAPI_CORU_RETURN
代码:
METHOD send_request.
DATA: lv_sysubrc TYPE sy-subrc,
lv_error_text TYPE string,
lv_result TYPE string,
lv_len TYPE i.
DATA: http_client TYPE REF TO if_http_client.
DATA: ls_return TYPE bapi_conf_return.
CLEAR:lv_result,
lv_len,
lv_sysubrc,
lv_error_text.
lv_len = strlen( iv_cdata ) .
"创建请求
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = iv_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
http_client->propertytype_logon_popup = http_client->co_disabled.
http_client->request->set_header_field(
EXPORTING
name = 'Content-Type'
value = 'application/json; charset=utf-8' ).
http_client->request->set_header_field(
EXPORTING
name = 'Accept'
value = 'application/json' ).
http_client->request->set_cdata(
EXPORTING
data = iv_cdata
offset = 0
length = lv_len ).
"设置http method
http_client->request->set_method( if_http_request=>co_request_method_post ).
"发送
CALL METHOD http_client->send
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
http_invalid_timeout = 4
OTHERS = 5.
IF sy-subrc <> 0.
CALL METHOD http_client->get_last_error
IMPORTING
code = lv_sysubrc
message = lv_error_text.
es_return-type = 'E'.
es_return-message = 'SEND DATA ERROR:' && lv_error_text.
ENDIF.
"接收
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3.
IF sy-subrc <> 0.
CALL METHOD http_client->get_last_error
IMPORTING
code = lv_sysubrc
message = lv_error_text.
es_return-type = 'E'.
es_return-message = 'RECEIVE DATA ERROR:' && lv_error_text.
ENDIF.
ev_result = http_client->response->get_cdata( ).
CALL METHOD http_client->close( ).
ENDMETHOD.
|