首页 > 解决方案 > 使用 PL/SQL-UTL_HTTP 代码将 PDF 文件发送到电报

问题描述

我正在尝试使用 Telegram bot 用户从我的服务器向 Telegram 组发送一个 pdf 文档和一个关于它的小描述。我可以使用 /sendDocument 方法自行发送文档。然而,这并不是我想要的。我想知道这甚至可能吗?

实际输出消息是“ORA-29263:HTTP 协议错误”。

declare
  l_attachment blob;
  l_newline varchar2(50) := chr(13) || chr(10);
  lco_boundary constant varchar2(30) := '----=*#abc1234321cba#*=';

  l_http_request utl_http.req;
  l_request_body clob;
  l_request_body_length number;

  l_http_response utl_http.resp;
  l_response_header_name varchar2(256);
  l_response_header_value varchar2(1024);
  l_response_body varchar2(32767);

  l_offset number := 1;
  l_amount number := 2000;
  l_buffer varchar2(2000);
  postData clob;
  p_id varchar2(50) := '********';
begin

select invoce into l_attachment  from some_table;

  l_request_body := l_newline
|| '--' || lco_boundary || l_newline
|| 'Content-Disposition: form-data; name="document"; filename="file.pdf"' || l_newline
|| 'Content-Type: application/pdf' || l_newline
|| l_newline
|| apex_web_service.blob2clobbase64(l_attachment) || l_newline
|| '--' || lco_boundary || l_newline
|| 'Content-Disposition: form-data; name="document"' || l_newline
|| l_newline
|| 'file.pdf' || l_newline
|| '--' || lco_boundary || l_newline
|| 'Content-Disposition: form-data; name="MAX_FILE_SIZE"' || l_newline
|| l_newline
|| '4000000' || l_newline
|| '--' || lco_boundary || '--';

  dbms_output.put_line('Request body>');
  dbms_output.put_line(dbms_lob.substr(l_request_body, 4000, 1));

  l_request_body_length := dbms_lob.getlength(l_request_body);

  utl_http.set_wallet('file:/u02/oracle/upload/WALLET_TELEGRAM/', '******');

  l_http_request := utl_http.begin_request(
                      url => 'https://api.telegram.org/bot<MY_KEY>/sendDocument',
                      method => 'POST',
                      http_version => 'HTTP/1.1'
                    );

  postData := postData || 'chat_id=' || p_id || '&';    
  postData := postData || 'document=';

  utl_http.set_header(l_http_request, 'Content-Type', 'multipart/form-data; boundary="' || lco_boundary || '"');
  utl_http.set_header(l_http_request, 'Content-Length', l_request_body_length);
  utl_http.write_text(l_http_request, postData);

  while l_offset < l_request_body_length loop
    dbms_lob.read(l_request_body, l_amount, l_offset, l_buffer);
    utl_http.write_text(l_http_request, l_buffer);
    l_offset := l_offset + l_amount;
  end loop;

  l_http_response := utl_http.get_response(l_http_request);

  for i in 1 .. utl_http.get_header_count(l_http_response) loop
    utl_http.get_header(l_http_response, i, l_response_header_name, l_response_header_value);
l_response_header_value);
  end loop;

  utl_http.read_text(l_http_response, l_response_body, 32767);
  dbms_output.put_line(l_response_body);

  if l_http_request.private_hndl is not null then
    utl_http.end_request(l_http_request);
  end if;

  if l_http_response.private_hndl is not null then
    utl_http.end_response(l_http_response);
  end if;
exception
  when others then
    if l_http_request.private_hndl is not null then
      utl_http.end_request(l_http_request);
    end if;

    if l_http_response.private_hndl is not null then
      utl_http.end_response(l_http_response);
    end if;

    raise;
end;

标签: oracleplsqltelegramtelegram-bot

解决方案


推荐阅读