diff --git a/src/Client.Helpers.cs b/src/Client.Helpers.cs index b8776ab..e7f0774 100644 --- a/src/Client.Helpers.cs +++ b/src/Client.Helpers.cs @@ -157,7 +157,6 @@ namespace WTelegram else updates = await this.Messages_SendMedia(peer, media, text, random_id, entities: entities, reply_to: reply_to_msg_id == 0 ? null : new InputReplyToMessage { reply_to_msg_id = reply_to_msg_id }, schedule_date: schedule_date == default ? null : schedule_date); - RaiseUpdate(updates); int msgId = -1; if (updates is UpdateShortSentMessage sent) return new Message @@ -250,7 +249,6 @@ namespace WTelegram if (entities != null) firstMedia.flags = InputSingleMedia.Flags.has_entities; var updates = await this.Messages_SendMultiMedia(peer, multiMedia, reply_to: reply_to_msg_id == 0 ? null : new InputReplyToMessage { reply_to_msg_id = reply_to_msg_id }, schedule_date: schedule_date); - RaiseUpdate(updates); var msgIds = new int[length]; var result = new Message[length]; foreach (var update in updates.UpdateList) diff --git a/src/Client.cs b/src/Client.cs index a9d43a9..05558f8 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -28,6 +28,8 @@ namespace WTelegram public event Func OnUpdate; /// This event is called for other types of notifications (login states, reactor errors, ...) public event Func OnOther; + /// Use this handler to intercept Updates that resulted from your own API calls + public event Func OnOwnUpdate; /// Used to create a TcpClient connected to the given address/port, or throw an exception on failure public TcpFactory TcpHandler { get; set; } = DefaultTcpHandler; public delegate Task TcpFactory(string host, int port); @@ -568,7 +570,12 @@ namespace WTelegram if (result is RpcError rpcError) Helpers.Log(4, $" → RpcError {rpcError.error_code,3} {rpcError.error_message,-24} #{(short)msgId.GetHashCode():X4}"); else + { Helpers.Log(1, $" → {result?.GetType().Name,-37} #{(short)msgId.GetHashCode():X4}"); + if (OnOwnUpdate != null && result is UpdatesBase updates) + RaiseOwnUpdate(updates); + } + rpc.tcs.SetResult(result); } catch (Exception ex) @@ -587,7 +594,13 @@ namespace WTelegram } else if (ctorNb == (uint)Bool.False) result = false; else if (ctorNb == (uint)Bool.True) result = true; - else result = reader.ReadTLObject(ctorNb); + else + { + result = reader.ReadTLObject(ctorNb); + if (OnOwnUpdate != null && result is UpdatesBase updates) + RaiseOwnUpdate(updates); + } + var typeName = result?.GetType().Name; if (MsgIdToStamp(msgId) >= _session.SessionStart) Helpers.Log(4, $" → {typeName,-37} for unknown msgId #{(short)msgId.GetHashCode():X4}"); @@ -734,6 +747,18 @@ namespace WTelegram } } + private async void RaiseOwnUpdate(UpdatesBase updates) + { + try + { + await OnOwnUpdate(updates); + } + catch (Exception ex) + { + Helpers.Log(4, $"{nameof(OnOwnUpdate)}({updates.GetType().Name}) raised {ex}"); + } + } + static async Task DefaultTcpHandler(string host, int port) { var tcpClient = new TcpClient();