Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Events Feature from PR #679 #892

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
251 changes: 251 additions & 0 deletions README.md
Expand Up @@ -143,6 +143,257 @@ To download file you should call **GetFile** method

Full code you can see at [DownloadFileFromContactTest](https://github.com/sochix/TLSharp/blob/master/TLSharp.Tests/TLSharpTests.cs#L167)

# Events Sample code
```csharp
using System;
using System.Threading.Tasks;
using TeleSharp.TL;
using TLSharp.Core;
using System.Linq;
using TeleSharp.TL.Messages;
using System.Collections.Generic;

namespace TLSharpPOC
{
class MainClass
{
const int APIId = 0;
const string APIHash = "???";
const string phone = "???";
public static void Main(string[] args)
{
new MainClass().MainAsync(args).Wait();
}

private async Task MainAsync(string[] args)
{
TelegramClient client = null;
try
{
// -- if necessary, IP can be changed so the client can connect to the test network.
Session session = null;
// new Session(new FileSessionStore(), "session")
//{
// ServerAddress = "149.154.175.10",
// Port = 443
//};
//Console.WriteLine($"{session.ServerAddress}:{session.Port} {phone}");
client = new TelegramClient(APIId, APIHash, session);
// subscribe an event to receive live messages
client.Updates += Client_Updates;
await client.ConnectAsync();
Console.WriteLine($"Authorised: {client.IsUserAuthorized()}");
TLUser user = null;
// -- If the user has already authenticated, this step will prevent account from being blocked as it
// -- reuses the data from last authorisation.
if (client.IsUserAuthorized())
user = client.Session.TLUser;
else
{
var registered = await client.IsPhoneRegisteredAsync(phone);
var hash = await client.SendCodeRequestAsync(phone);
Console.Write("Code: ");
var code = Console.ReadLine();
if (!registered)
{
Console.WriteLine($"Sign up {phone}");
user = await client.SignUpAsync(phone, hash, code, "First", "Last");
}
Console.WriteLine($"Sign in {phone}");
user = await client.MakeAuthAsync(phone, hash, code);
}

var contacts = await client.GetContactsAsync();
Console.WriteLine("Contacts:");
foreach (var contact in contacts.Users.OfType<TLUser>())
{
var contactUser = contact as TLUser;
Console.WriteLine($"\t{contact.Id} {contact.Phone} {contact.FirstName} {contact.LastName}");
}


var dialogs = (TLDialogs) await client.GetUserDialogsAsync();
Console.WriteLine("Channels: ");
foreach (var channelObj in dialogs.Chats.OfType<TLChannel>())
{
var channel = channelObj as TLChannel;
Console.WriteLine($"\tChat: {channel.Title}");
}

Console.WriteLine("Groups:");
TLChat chat = null;
foreach (var chatObj in dialogs.Chats.OfType<TLChat>())
{
chat = chatObj as TLChat;
Console.WriteLine($"Chat name: {chat.Title}");
var request = new TLRequestGetFullChat() { ChatId = chat.Id };
var fullChat = await client.SendRequestAsync<TeleSharp.TL.Messages.TLChatFull>(request);

var participants = (fullChat.FullChat as TeleSharp.TL.TLChatFull).Participants as TLChatParticipants;
foreach (var p in participants.Participants)
{
if (p is TLChatParticipant)
{
var participant = p as TLChatParticipant;
Console.WriteLine($"\t{participant.UserId}");
}
else if (p is TLChatParticipantAdmin)
{
var participant = p as TLChatParticipantAdmin;
Console.WriteLine($"\t{participant.UserId}**");
}
else if (p is TLChatParticipantCreator)
{
var participant = p as TLChatParticipantCreator;
Console.WriteLine($"\t{participant.UserId}**");
}
}

var peer = new TLInputPeerChat() { ChatId = chat.Id };
var m = await client.GetHistoryAsync(peer, 0, 0, 0);
Console.WriteLine(m);
if (m is TLMessages)
{
var messages = m as TLMessages;


foreach (var message in messages.Messages)
{
if (message is TLMessage)
{
var m1 = message as TLMessage;
Console.WriteLine($"\t\t{m1.Id} {m1.Message}");
}
else if (message is TLMessageService)
{
var m1 = message as TLMessageService;
Console.WriteLine($"\t\t{m1.Id} {m1.Action}");
}
}
}
else if (m is TLMessagesSlice)
{
bool done = false;
int total = 0;
while (!done)
{
var messages = m as TLMessagesSlice;

foreach (var m1 in messages.Messages)
{
if (m1 is TLMessage)
{
var message = m1 as TLMessage;
Console.WriteLine($"\t\t{message.Id} {message.Message}");
++total;
}
else if (m1 is TLMessageService)
{
var message = m1 as TLMessageService;
Console.WriteLine($"\t\t{message.Id} {message.Action}");
++total;
done = message.Action is TLMessageActionChatCreate;
}
}
m = await client.GetHistoryAsync(peer, total, 0, 0);
}
}
}

// -- Wait in a loop to handle incoming updates. No need to poll.
for (;;)
{
await client.WaitEventAsync();
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

private void Client_Updates(TelegramClient client, TLAbsUpdates updates)
{
Console.WriteLine($"Got update: {updates}");
if (updates is TLUpdateShort)
{
var updateShort = updates as TLUpdateShort;
Console.WriteLine($"Short: {updateShort.Update}");
if (updateShort.Update is TLUpdateUserStatus)
{
var status = updateShort.Update as TLUpdateUserStatus;
Console.WriteLine($"User {status.UserId} is {status.Status}");
if (status.Status is TLUserStatusOnline)
{
try
{
var peer = new TLInputPeerUser() { UserId = status.UserId };
client.SendMessageAsync(peer, "Você está online.").Wait();
} catch {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this catch please

}
}
}
else if (updates is TLUpdateShortMessage)
{
var message = updates as TLUpdateShortMessage;
Console.WriteLine($"Message: {message.Message}");
MarkMessageRead(client, new TLInputPeerUser() { UserId = message.UserId }, message.Id);
}
else if (updates is TLUpdateShortChatMessage)
{
var message = updates as TLUpdateShortChatMessage;
Console.WriteLine($"Chat Message: {message.Message}");
MarkMessageRead(client, new TLInputPeerChat() { ChatId = message.ChatId }, message.Id);
}
else if (updates is TLUpdates)
{
var allUpdates = updates as TLUpdates;
foreach (var update in allUpdates.Updates)
{
Console.WriteLine($"\t{update}");
if (update is TLUpdateNewChannelMessage)
{
var metaMessage = update as TLUpdateNewChannelMessage;
var message = metaMessage.Message as TLMessage;
Console.WriteLine($"Channel message: {message.Message}");
var channel = allUpdates.Chats[0] as TLChannel;
MarkMessageRead(client,
new TLInputPeerChannel() { ChannelId = channel.Id, AccessHash = channel.AccessHash.Value },
message.Id );
}
}

foreach(var user in allUpdates.Users)
{
Console.WriteLine($"{user}");
}

foreach (var chat in allUpdates.Chats)
{
Console.WriteLine($"{chat}");
}
}
}

private void MarkMessageRead(TelegramClient client, TLAbsInputPeer peer, int id)
{
// An exception happens here but it's not fatal.
try
{
var request = new TLRequestReadHistory();
request.MaxId = id;
request.Peer = peer;
client.SendRequestAsync<bool>(request).Wait();
}
catch (InvalidOperationException e){
System.Console.WriteLine(e.getMessage())
}

}
}
}
```

# Available Methods

For your convenience TLSharp have wrappers for several Telegram API methods. You could add your own, see details below.
Expand Down
69 changes: 69 additions & 0 deletions TLSharp.Core/Network/Exceptions.cs
@@ -0,0 +1,69 @@
using System;
namespace TLSharp.Core.Network
{
public class FloodException : Exception
{
public TimeSpan TimeToWait { get; private set; }

internal FloodException(TimeSpan timeToWait)
: base($"Flood prevention. Telegram now requires your program to do requests again only after {timeToWait.TotalSeconds} seconds have passed ({nameof(TimeToWait)} property)." +
" If you think the culprit of this problem may lie in TLSharp's implementation, open a Github issue please.")
{
TimeToWait = timeToWait;
}
}

public class BadMessageException : Exception
{
internal BadMessageException(string description) : base(description)
{
}
}

internal abstract class DataCenterMigrationException : Exception
{
internal int DC { get; private set; }

private const string REPORT_MESSAGE =
" See: https://github.com/sochix/TLSharp#i-get-a-xxxmigrationexception-or-a-migrate_x-error";

protected DataCenterMigrationException(string msg, int dc) : base(msg + REPORT_MESSAGE)
{
DC = dc;
}
}

internal class PhoneMigrationException : DataCenterMigrationException
{
internal PhoneMigrationException(int dc)
: base($"Phone number registered to a different DC: {dc}.", dc)
{
}
}

internal class FileMigrationException : DataCenterMigrationException
{
internal FileMigrationException(int dc)
: base($"File located on a different DC: {dc}.", dc)
{
}
}

internal class UserMigrationException : DataCenterMigrationException
{
internal UserMigrationException(int dc)
: base($"User located on a different DC: {dc}.", dc)
{
}
}

internal class NetworkMigrationException : DataCenterMigrationException
{
internal NetworkMigrationException(int dc)
: base($"Network located on a different DC: {dc}.", dc)
{
}
}


}