Skip to content

Commit

Permalink
added support for topic/foums
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Muhandis committed Mar 10, 2024
1 parent 11d254a commit bd0e9cb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion fptelegram.lpk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</CompilerOptions>
<Description Value="Telegram bots API library"/>
<License Value="MIT License"/>
<Version Release="3"/>
<Version Release="3" Build="1"/>
<Files Count="6">
<Item1>
<Filename Value="tgsendertypes.pas"/>
Expand Down
9 changes: 7 additions & 2 deletions test/console.lpi
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="11"/>
<Version Value="12"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
<CompatibilityMode Value="True"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="console"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
Expand Down
50 changes: 38 additions & 12 deletions tgsendertypes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ TTelegramSender = class
FBotUsername: String;
FCurrentChat: TTelegramChatObj;
FCurrentChatId: Int64;
FCurrentIsTopicMessage: Boolean;
FCurrentThreadId: Integer;
FCurrentMessage: TTelegramMessageObj;
FCurrentUser: TTelegramUserObj;
FBotUser: TTelegramUserObj;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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', '');
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -2234,6 +2250,8 @@ procedure TTelegramSender.DoReceiveUpdate(AnUpdate: TTelegramUpdateObj);
FUpdate:=AnUpdate;
FCurrentMessage:=nil;
FCurrentChatId:=0;
FCurrentThreadId:=_nullThrd;
FCurrentIsTopicMessage:=False;
FCurrentUser:=nil;
FCurrentChat:=nil;
FLanguage:='';
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions tgtypes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bd0e9cb

Please sign in to comment.