Skip to content

Commit

Permalink
Document
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Muhandis committed Jul 21, 2019
1 parent 2e72d19 commit 1e786fd
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
50 changes: 48 additions & 2 deletions tgsendertypes.pas
Expand Up @@ -10,7 +10,8 @@ interface
type
TParseMode = (pmDefault, pmMarkdown, pmHTML);
TMediaType = (mtPhoto, mtVideo, mtUnknown);
TInlineQueryResultType = (qrtArticle, qrtPhoto, qrtVideo, qrtAudio, qrtVoice, qrtMpeg4Gif, qrtUnknown);
TInlineQueryResultType = (qrtArticle, qrtPhoto, qrtVideo, qrtAudio, qrtVoice, qrtMpeg4Gif,
qrtDocument, qrtUnknown);
TLogMessageEvent = procedure(ASender: TObject; EventType: TEventType; const Msg: String) of object;
TInlineKeyboardButton = class;
TKeyboardButton = class;
Expand Down Expand Up @@ -195,6 +196,7 @@ TInlineQueryResult = class(TJSONObject)
function GetAudioUrl: String;
function GetCaption: String;
function GetDescription: String;
function GetDocumentFileID: String;
function GetID: String;
function GetInputMessageContent: TInputMessageContent;
function GetIQRType: TInlineQueryResultType;
Expand All @@ -219,6 +221,7 @@ TInlineQueryResult = class(TJSONObject)
procedure SetAudioUrl(AValue: String);
procedure SetCaption(AValue: String);
procedure SetDescription(AValue: String);
procedure SetDocumentFileID(AValue: String);
procedure SetID(AValue: String);
procedure SetInputMessageContent(AValue: TInputMessageContent);
procedure SetIQRType(AValue: TInlineQueryResultType);
Expand Down Expand Up @@ -247,6 +250,7 @@ TInlineQueryResult = class(TJSONObject)
property Description: String read GetDescription write SetDescription;

property AudioFileID: String read GetAudioFileID write SetAudioFileID;
property DocumentFileID: String read GetDocumentFileID write SetDocumentFileID;
property PhotoFileID: String read GetPhotoFileID write SetPhotoFileID;
property VideoFileID: String read GetVideoFileID write SetVideoFileID;
property VoiceFileID: String read GetVoiceFileID write SetVoiceFileID;
Expand Down Expand Up @@ -492,6 +496,9 @@ TTelegramSender = class
ParseMode: TParseMode = pmDefault; Duration: Integer = 0; DisableNotification: Boolean = False;
ReplyToMessageID: Integer = 0; const Performer:String = ''; const Title: String = '';
ReplyMarkup: TReplyMarkup = nil): Boolean;
function sendDocument(chat_id: Int64; const file_id: String; const Caption: String = '';
ParseMode: TParseMode = pmDefault; DisableNotification: Boolean = False;
ReplyToMessageID: Integer = 0; ReplyMarkup: TReplyMarkup = nil): Boolean;
function sendDocumentByFileName(chat_id: Int64; const AFileName: String;
const ACaption: String; ReplyMarkup: TReplyMarkup = nil): Boolean;
function sendDocumentStream(chat_id: Int64; const AFileName: String; ADocStream: TStream;
Expand Down Expand Up @@ -723,6 +730,7 @@ implementation
s_PhotoWidth = 'photo_width';
s_PhotoSize = 'photo_size';
s_AudioFileID = 'audio_file_id';
s_DocumentFileID = 'document_file_id';
s_PhotoFileID = 'photo_file_id';
s_VideoFileID = 'video_file_id';
s_VoiceFileID = 'voice_file_id';
Expand All @@ -747,7 +755,7 @@ implementation
ParseModes: array[TParseMode] of PChar = ('', 'Markdown', 'HTML');
MediaTypes: array[TMediaType] of PChar = ('photo', 'video', '');
QueryResultTypeArray: array[TInlineQueryResultType] of PChar =
('article', 'photo', 'video', 'audio', 'voice', 'mpeg4_gif', '');
('article', 'photo', 'video', 'audio', 'voice', 'mpeg4_gif', 'document', '');

TgBotUrlStart = 'https://t.me/';

Expand Down Expand Up @@ -1103,6 +1111,11 @@ function TInlineQueryResult.GetDescription: String;
Result:=Get(s_Description, EmptyStr);
end;

function TInlineQueryResult.GetDocumentFileID: String;
begin
Result:=Get(s_DocumentFileID, EmptyStr);
end;

function TInlineQueryResult.GetID: String;
begin
Result:=Strings[s_ID];
Expand Down Expand Up @@ -1223,6 +1236,11 @@ procedure TInlineQueryResult.SetDescription(AValue: String);
Strings[s_Description]:=AValue;
end;

procedure TInlineQueryResult.SetDocumentFileID(AValue: String);
begin
Strings[s_DocumentFileID]:=AValue;
end;

procedure TInlineQueryResult.SetID(AValue: String);
begin
Strings[s_ID]:=AValue;
Expand Down Expand Up @@ -2519,6 +2537,34 @@ function TTelegramSender.SendAudio(chat_id: Int64; const audio: String;
end;
end;

function TTelegramSender.sendDocument(chat_id: Int64; const file_id: String;
const Caption: String; ParseMode: TParseMode; DisableNotification: Boolean;
ReplyToMessageID: Integer; ReplyMarkup: TReplyMarkup): Boolean;
var
sendObj: TJSONObject;
begin
Result:=False;
sendObj:=TJSONObject.Create;
with sendObj do
try
Add(s_ChatId, chat_id);
Add(s_Document, file_id);
if Caption<>EmptyStr then
Add(s_Caption, Caption);
if ParseMode<>pmDefault then
Add(s_ParseMode, ParseModes[ParseMode]);
if DisableNotification then
Add(s_DsblNtfctn, DisableNotification);
if ReplyToMessageID<>0 then
Add(s_ReplyToMessageID, ReplyToMessageID);
if Assigned(ReplyMarkup) then
Add(s_ReplyMarkup, ReplyMarkup.Clone); // Clone of ReplyMarkup object will have released with sendObject
Result:=SendMethod(s_sendDocument, sendObj);
finally
Free;
end;
end;

function TTelegramSender.sendDocumentByFileName(chat_id: Int64; const AFileName: String;
const ACaption: String; ReplyMarkup: TReplyMarkup): Boolean;
var
Expand Down
43 changes: 42 additions & 1 deletion tgtypes.pas
Expand Up @@ -26,6 +26,7 @@ TTelegramSuccessfulPayment = class;
TTelegramVideo = class;
TTelegramVoice = class;
TTelegramAudio = class;
TTelegramDocument = class;

TUpdateType = (utMessage, utEditedMessage, utChannelPost, utEditedChannelPost, utInlineQuery,
utChosenInlineResult, utCallbackQuery, utShippingQuery, utPreCheckoutQuery, utUnknown);
Expand Down Expand Up @@ -86,6 +87,7 @@ TTelegramMessageObj = class(TTelegramObj)
FAudio: TTelegramAudio;
FCaption: String;
FChat: TTelegramChatObj;
FDocument: TTelegramDocument;
FFrom: TTelegramUserObj;
FLocation: TTelegramLocation;
fMessageId: Integer;
Expand All @@ -108,12 +110,13 @@ TTelegramMessageObj = class(TTelegramObj)
property ReplyToMessage: TTelegramMessageObj read FReplyToMessage;
property Text: string read fText;
property Entities: TTelegramUpdateObjList read fEntities;
property Document: TTelegramDocument read FDocument;
property Location: TTelegramLocation read FLocation;
property Photo: TTelegramPhotoSizeList read FPhoto;
property SuccessfulPayment: TTelegramSuccessfulPayment read FSuccessfulPayment;
property Audio: TTelegramAudio read FAudio;
property Video: TTelegramVideo read FVideo;
property Voice: TTelegramVoice read FVoice;
property SuccessfulPayment: TTelegramSuccessfulPayment read FSuccessfulPayment;
end;

{ TTelegramMessageEntityObj }
Expand Down Expand Up @@ -377,6 +380,25 @@ TTelegramVideo = class(TTelegramObj)
property FileSize: Integer read FFileSize;
end;


{ TTelegramDocument }

TTelegramDocument = class(TTelegramObj)
private
FDuration: Integer;
FFileID: String;
FFileSize: Integer;
FMimeType: String;
FThumb: TTelegramPhotoSize;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property FileID: String read FFileID;
property MimeType: String read FMimeType;
property FileSize: Integer read FFileSize;
property Thumb: TTelegramPhotoSize read FThumb;
end;

{ TTelegramAudio }

TTelegramAudio = class(TTelegramObj)
Expand Down Expand Up @@ -444,6 +466,23 @@ function AllowedUpdatesToJSON(const AllowedUpdates: TUpdateSet): TJSONArray;
Result.Add(UpdateTypeAliases[u]);
end;

{ TTelegramDocument }

constructor TTelegramDocument.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FFileID := fJSON.Strings['file_id'];
FThumb:=TTelegramPhotoSize.CreateFromJSONObject(fJSON.Find('thumb', jtObject) as TJSONObject) as TTelegramPhotoSize;
FMimeType:=fJSON.Get('mime_type', EmptyStr);
FFileSize := fJSON.Get('file_size', 0);
end;

destructor TTelegramDocument.Destroy;
begin
FThumb.Free;
inherited Destroy;
end;

{ TTelegramAudio }

constructor TTelegramAudio.Create(JSONObject: TJSONObject);
Expand Down Expand Up @@ -901,6 +940,7 @@ constructor TTelegramMessageObj.Create(JSONObject: TJSONObject);
FChat:=TTelegramChatObj.CreateFromJSONObject(fJSON.Find('chat', jtObject) as TJSONObject) as TTelegramChatObj;
fChatId := fJSON.Objects['chat'].Int64s['id']; // deprecated?
FFrom:=TTelegramUserObj.CreateFromJSONObject(fJSON.Find('from', jtObject) as TJSONObject) as TTelegramUserObj;
FDocument := TTelegramDocument.CreateFromJSONObject(fJSON.Find('document', jtObject) as TJSONObject) as TTelegramDocument;
FVideo := TTelegramVideo.CreateFromJSONObject(fJSON.Find('video', jtObject) as TJSONObject) as TTelegramVideo;
FAudio := TTelegramAudio.CreateFromJSONObject(fJSON.Find('audio', jtObject) as TJSONObject) as TTelegramAudio;
FVoice := TTelegramVoice.CreateFromJSONObject(fJSON.Find('voice', jtObject) as TJSONObject) as TTelegramVoice;
Expand Down Expand Up @@ -935,6 +975,7 @@ destructor TTelegramMessageObj.Destroy;
FChat.Free;
FReplyToMessage.Free;
FPhoto.Free;
FDocument.Free;
FAudio.Free;
FVideo.Free;
FVoice.Free;
Expand Down

0 comments on commit 1e786fd

Please sign in to comment.