From bd0e9cb5a751b1eaf290f9ec172c4129da24e775 Mon Sep 17 00:00:00 2001 From: Renat Suleymanov Date: Sun, 10 Mar 2024 22:19:58 +0400 Subject: [PATCH] added support for topic/foums --- fptelegram.lpk | 2 +- test/console.lpi | 9 +++++++-- tgsendertypes.pas | 50 +++++++++++++++++++++++++++++++++++------------ tgtypes.pas | 10 ++++++++++ 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/fptelegram.lpk b/fptelegram.lpk index 750be5a..9f6a8ca 100644 --- a/fptelegram.lpk +++ b/fptelegram.lpk @@ -23,7 +23,7 @@ - + diff --git a/test/console.lpi b/test/console.lpi index 46f6cb3..cfc67d2 100644 --- a/test/console.lpi +++ b/test/console.lpi @@ -1,11 +1,16 @@ - + + + + + + + - <UseAppBundle Value="False"/> <ResourceType Value="res"/> diff --git a/tgsendertypes.pas b/tgsendertypes.pas index e7e5292..318819d 100644 --- a/tgsendertypes.pas +++ b/tgsendertypes.pas @@ -423,6 +423,8 @@ TTelegramSender = class FBotUsername: String; FCurrentChat: TTelegramChatObj; FCurrentChatId: Int64; + FCurrentIsTopicMessage: Boolean; + FCurrentThreadId: Integer; FCurrentMessage: TTelegramMessageObj; FCurrentUser: TTelegramUserObj; FBotUser: TTelegramUserObj; @@ -607,7 +609,7 @@ TTelegramSender = class function sendMediaGroupByStreams(chat_id: Int64; const ACaption: String; StreamsList:TStringList): Boolean; function sendMessage(chat_id: Int64; const AMessage: String; ParseMode: TParseMode = pmDefault; DisableWebPagePreview: Boolean=False; ReplyMarkup: TReplyMarkup = nil; - ReplyToMessageID: Integer = 0; DisableNotification: Boolean = False): Boolean; + ReplyToMessageID: Integer = 0; DisableNotification: Boolean = False; MessageThreadID: Integer = _nullThrd): Boolean; function sendMessageChannel(const chat_id, AMessage: String; ParseMode: TParseMode = pmDefault; DisableWebPagePreview: Boolean=False; ReplyMarkup: TReplyMarkup = nil; ReplyToMessageID: Integer = 0): Boolean; @@ -919,8 +921,7 @@ implementation s_Cht='chat'; s_ChtAdmnstrtrs='chat_administrators'; s_ChtMmbr='chat_member'; - - + s_MsgThrdID ='message_thread_id'; ParseModes: array[TParseMode] of PChar = ('', 'Markdown', 'HTML'); MediaTypes: array[TMediaType] of PChar = ('photo', 'video', ''); @@ -2080,6 +2081,8 @@ procedure TTelegramSender.DoReceiveMessageUpdate(AMessage: TTelegramMessageObj); begin FCurrentMessage:=AMessage; FCurrentChatID:=AMessage.ChatId; + FCurrentThreadId:=AMessage.MessageThreadID; + FCurrentIsTopicMessage:=AMessage.IsTopicMessage; FCurrentChat:=AMessage.Chat; FCurrentUser:=AMessage.From; if CurrentIsBanned then @@ -2098,6 +2101,8 @@ procedure TTelegramSender.DoReceiveEditedMessage(AMessage: TTelegramMessageObj); begin FCurrentMessage:=AMessage; FCurrentChatID:=AMessage.ChatId; + FCurrentThreadId:=AMessage.MessageThreadID; + FCurrentIsTopicMessage:=AMessage.IsTopicMessage; FCurrentChat:=AMessage.Chat; FCurrentUser:=AMessage.From; if CurrentIsBanned then @@ -2117,6 +2122,8 @@ procedure TTelegramSender.DoReceiveCallbackQuery(ACallback: TCallbackQueryObj); FCurrentChat:=FCurrentMessage.Chat; FCurrentUser:=ACallback.From; FCurrentChatID:=FCurrentUser.ID; { Bot will send to private chat if in channel is called } {ACallback.Message.ChatId;} + FCurrentThreadId:=_nullThrd; + FCurrentIsTopicMessage:=False; if CurrentIsBanned then Exit; if Assigned(FCurrentMessage) then @@ -2133,7 +2140,9 @@ procedure TTelegramSender.DoReceiveChannelPost(AChannelPost: TTelegramMessageObj begin FCurrentMessage:=AChannelPost; FCurrentChat:=AChannelPost.Chat; - FCurrentChatID:=AChannelPost.ChatId; + FCurrentChatID:=AChannelPost.ChatId; + FCurrentThreadId:=AChannelPost.MessageThreadID; + FCurrentIsTopicMessage:=AChannelPost.IsTopicMessage; FCurrentUser:=AChannelPost.From; if CurrentIsBanned then Exit; @@ -2145,12 +2154,13 @@ procedure TTelegramSender.DoReceiveChannelPost(AChannelPost: TTelegramMessageObj FOnReceiveChannelPost(Self, AChannelPost); end; -procedure TTelegramSender.DoReceiveEditedChannelPost( - AChannelPost: TTelegramMessageObj); +procedure TTelegramSender.DoReceiveEditedChannelPost(AChannelPost: TTelegramMessageObj); begin FCurrentMessage:=AChannelPost; FCurrentChat:=AChannelPost.Chat; - FCurrentChatID:=AChannelPost.ChatId; + FCurrentChatID:=AChannelPost.ChatId; + FCurrentThreadId:=AChannelPost.MessageThreadID; + FCurrentIsTopicMessage:=AChannelPost.IsTopicMessage; FCurrentUser:=AChannelPost.From; if CurrentIsBanned then Exit; @@ -2168,6 +2178,8 @@ procedure TTelegramSender.DoReceiveInlineQuery( FCurrentMessage:=nil; FCurrentChat:=nil; FCurrentChatID:=AnInlineQuery.From.ID; + FCurrentThreadId:=_nullThrd; + FCurrentIsTopicMessage:=False; FCurrentUser:=AnInlineQuery.From; if CurrentIsBanned then Exit; @@ -2183,7 +2195,9 @@ procedure TTelegramSender.DoReceiveChosenInlineResult( begin FCurrentMessage:=nil; FCurrentChat:=nil; - FCurrentChatID:=AChosenInlineResult.From.ID; + FCurrentChatID:=AChosenInlineResult.From.ID; + FCurrentThreadId:=_nullThrd; + FCurrentIsTopicMessage:=False; FCurrentUser:=AChosenInlineResult.From; if CurrentIsBanned then Exit; @@ -2198,7 +2212,9 @@ procedure TTelegramSender.DoReceivePreCheckoutQuery( APreCheckoutQuery: TTelegramPreCheckOutQuery); begin FCurrentUser:=APreCheckoutQuery.From; - FCurrentChatID:=FCurrentUser.ID; + FCurrentChatID:=FCurrentUser.ID; + FCurrentThreadId:=_nullThrd; + FCurrentIsTopicMessage:=False; if CurrentIsBanned then Exit; DoAfterParseUpdate; @@ -2234,6 +2250,8 @@ procedure TTelegramSender.DoReceiveUpdate(AnUpdate: TTelegramUpdateObj); FUpdate:=AnUpdate; FCurrentMessage:=nil; FCurrentChatId:=0; + FCurrentThreadId:=_nullThrd; + FCurrentIsTopicMessage:=False; FCurrentUser:=nil; FCurrentChat:=nil; FLanguage:=''; @@ -3503,8 +3521,8 @@ function TTelegramSender.sendMediaGroupByStreams(chat_id: Int64; { https://core.telegram.org/bots/api#sendmessage } function TTelegramSender.sendMessage(chat_id: Int64; const AMessage: String; ParseMode: TParseMode; - DisableWebPagePreview: Boolean; ReplyMarkup: TReplyMarkup; ReplyToMessageID: Integer; DisableNotification: Boolean - ): Boolean; + DisableWebPagePreview: Boolean; ReplyMarkup: TReplyMarkup; ReplyToMessageID: Integer; DisableNotification: Boolean; + MessageThreadID: Integer): Boolean; var sendObj: TJSONObject; begin @@ -3522,6 +3540,8 @@ function TTelegramSender.sendMessage(chat_id: Int64; const AMessage: String; Par Add(s_ReplyMarkup, ReplyMarkup.Clone); // Clone of ReplyMarkup object will have released with sendObject if ReplyToMessageID<>0 then Add(s_ReplyToMessageID, ReplyToMessageID); + if MessageThreadID<>_nullThrd then + Add(s_MsgThrdID, MessageThreadID); Result:=SendMethod(s_sendMessage, sendObj); finally Free; @@ -3555,9 +3575,15 @@ function TTelegramSender.sendMessageChannel(const chat_id, AMessage: String; function TTelegramSender.sendMessage(const AMessage: String; ParseMode: TParseMode; DisableWebPagePreview: Boolean; ReplyMarkup: TReplyMarkup; ReplyToMessageID: Integer; DisableNotification: Boolean): Boolean; +var + aMsgThrdID: Integer; begin + if FCurrentIsTopicMessage then // Need for the checking is questionable + aMsgThrdID:=FCurrentThreadId + else + aMsgThrdID:=_nullThrd; Result:=sendMessage(FCurrentChatId, AMessage, ParseMode, DisableWebPagePreview, ReplyMarkup, - ReplyToMessageID, DisableNotification); + ReplyToMessageID, DisableNotification, aMsgThrdID); end; { https://core.telegram.org/bots/api#sendphoto } diff --git a/tgtypes.pas b/tgtypes.pas index a807832..1a5d41d 100644 --- a/tgtypes.pas +++ b/tgtypes.pas @@ -98,10 +98,12 @@ TTelegramMessageObj = class(TTelegramObj) FForwardFromChat: TTelegramChatObj; FForwardFromMessageID: LongInt; FFrom: TTelegramUserObj; + FIsTopicMessage: Boolean; FLocation: TTelegramLocation; FMediaGroupID: String; fMessageId: Integer; fChatId: Int64; + FMessageThreadID: Integer; FPhoto: TTelegramPhotoSizeList; FReplyToMessage: TTelegramMessageObj; FSuccessfulPayment: TTelegramSuccessfulPayment; @@ -136,6 +138,8 @@ TTelegramMessageObj = class(TTelegramObj) property SuccessfulPayment: TTelegramSuccessfulPayment read FSuccessfulPayment; property ViaBot: TTelegramUserObj read FViaBot; property MediaGroupID: String read FMediaGroupID; + property MessageThreadID: Integer read FMessageThreadID; + property IsTopicMessage: Boolean read FIsTopicMessage; end; { TTelegramMessageEntityObj } @@ -515,6 +519,7 @@ TTelegramObjClass = class of TTelegramObj; TTelegramMessageObj, TTelegramMessageObj, TTelegramMessageObj, TTelegramInlineQueryObj, TTelegramChosenInlineResultObj, TCallbackQueryObj, TTelegramObj, TTelegramPreCheckOutQuery, TTelegramObj, TTelegramObj, TTelegramObj); + _nullThrd = 0; function AllowedUpdatesToJSON(const AllowedUpdates: TUpdateSet): TJSONArray; @@ -530,6 +535,8 @@ implementation s_FileSize = 'file_size'; s_FileName = 'file_name'; + s_IsTpcMsg = 'is_topic_message'; + s_MsgThrdID ='message_thread_id'; function AllowedUpdatesToJSON(const AllowedUpdates: TUpdateSet): TJSONArray; var @@ -1069,6 +1076,9 @@ constructor TTelegramMessageObj.Create(JSONObject: TJSONObject); inherited Create(JSONObject); fMessageId := fJSON.Integers['message_id']; + FIsTopicMessage:=fJSON.Get(s_IsTpcMsg, False); + FMessageThreadID:=fJSON.Get(s_MsgThrdID, 0); + fText := fJSON.Get('text', EmptyStr); fEntities := TTelegramUpdateObjList.Create; FPhoto := TTelegramPhotoSizeList.Create;