OnOwnUpdate handler can be used to intercept Updates replies to your API calls

This commit is contained in:
Wizou 2023-11-30 16:06:37 +01:00
parent 8f6e6440ba
commit b6c98658db
2 changed files with 26 additions and 3 deletions

View file

@ -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)

View file

@ -28,6 +28,8 @@ namespace WTelegram
public event Func<UpdatesBase, Task> OnUpdate;
/// <summary>This event is called for other types of notifications (login states, reactor errors, ...)</summary>
public event Func<IObject, Task> OnOther;
/// <summary>Use this handler to intercept Updates that resulted from your own API calls</summary>
public event Func<UpdatesBase, Task> OnOwnUpdate;
/// <summary>Used to create a TcpClient connected to the given address/port, or throw an exception on failure</summary>
public TcpFactory TcpHandler { get; set; } = DefaultTcpHandler;
public delegate Task<TcpClient> 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<TcpClient> DefaultTcpHandler(string host, int port)
{
var tcpClient = new TcpClient();