You are not logged in.
Hello,
I just try to communicate with telegram bot api sendmessage using THttpClientSocket.
lHttp := THttpClientSocket.Create;
LParam := THttpMultiPartStream.Create;
lURL_TELEG := 'https://api.telegram.org/bot' + FToken + '/sendmessage';
LUri.From(lURL_TELEG);
lHttp.Open(LUri.Server, LUri.Port, nlTcp, 10000, LUri.Https);
LParam.AddContent('chat_id', chat_id);
LParam.AddContent('text', text);
lHttpResponse := lHttp.Post(LUri.Address, LParam, 'multipart/formdata');
And I received the response error 400 which is "Bad Request: message text is empty".
But I checked that text = 'sample text' not empty.
Am i do right usage THttpClientSocket and THttpMultiPartStream?
Offline
...
LParam.AddContent('text', text);
LParam.Flush;
lHttpResponse := lHttp.Post(LUri.Address, LParam, 'multipart/formdata');
...
The result is same, error code 400.
And i tried to use sendphoto method of the telegram bot API as blow:
...
LParam.AddContent('chat_id', FloatToStr(ChatID));
LParam.AddContent('bmp', 'image/bmp');
LParam.AddFileContent('photo', LPath, LFile);
// LParam.AddFile('photo', LPath);
LParam.Flush();
lHttpResponse := lHttp.Post(LUri.Address, LParam, 'multipart/formdata');
...
And i received the result error code 400 : "Bad Request: there is no photo in the request"
I think, I don't seem to know how to use THttpMultiPartStream and THttpClientSocket.Post function.
Please give me some advice.
Last edited by bigheart (2024-02-11 04:29:39)
Offline
I think, I don't seem to know how to use THttpMultiPartStream and THttpClientSocket.Post function.
Please give me some advice.
Did you watch the stream before sending it? The following simple test looks ok to me:
var
mpStream: THttpMultiPartStream;
fileStream: THandleStream;
begin
mpStream := THttpMultiPartStream.Create;
try
mpStream.AddContent('Field01', '100', TEXT_CONTENT_TYPE);
mpStream.AddContent('Field02', '{"Name": "Thomas"}', JSON_CONTENT_TYPE);
mpStream.AddFileContent('Data', 'data.json', '{"Name": "Thomas","Nickname": "tbo"}', JSON_CONTENT_TYPE, 'binary');
mpStream.AddFile('Test', MakePath([Executable.ProgramFilePath, 'test.json']));
mpStream.AddFile('Image', MakePath([Executable.ProgramFilePath, 'image.png']));
fileStream := TFileStreamEx.Create(MakePath([Executable.ProgramFilePath, 'mpTest.dat']), fmCreate);
try
mpStream.Flush;
StreamCopyUntilEnd(mpStream, fileStream);
finally
fileStream.Free;
end;
finally
mpStream.Free;
end;
With best regards
Thomas
Offline
This code works for me:
function SendTgDoc(const Token, Chat, Text: RawUtf8;
const Doc: RawByteString): boolean;
var
LMultiPart: THttpMultiPartStream;
LContentType, LData: RawUtf8;
LClient: THttpClientSocket;
begin
LMultiPart := THttpMultiPartStream.Create();
try
LMultiPart.AddContent('chat_id', Chat, 'text/plain');
LMultiPart.AddContent('caption', Text, 'text/plain');
LMultiPart.AddFileContent('document', 'attach.pdf', Doc, 'application/pdf', 'binary');
LMultiPart.Flush();
LClient := THttpClientSocket.Open('api.telegram.org', '443', nlTcp, 5000, True);
try
LClient.Post('/bot' + Token + '/sendDocument',
LMultiPart, LMultiPart.MultipartContentType);
finally
LClient.Free();
end;
finally
LMultiPart.Free();
end;
end;
Offline
Thank you chaa.
It works on your code.
Offline