Jaume
Jaume

Reputation: 3780

C# How to get messages from channel/contact using Telegram API (not Bot API)

I found many examples using the bot api, however I need a simple client that implements an event when a message from a contact or group is received, as a user and not a bot (so the Telegram api, and not Bot api). TLSharp library doesn't implement this method. What is the best way to achieve it?

Upvotes: 3

Views: 8461

Answers (2)

Wizou
Wizou

Reputation: 1897

There is now WTelegramClient, using the latest Telegram Client API protocol (connecting as a user, not bot).

The library is very complete but also very simple to use. Follow the README on GitHub for an easy introduction.

To monitor Update events that are pushed to the client whenever a message is posted somewhere (or other events), take a look at the Examples\Program_ListenUpdate.cs. It demonstrates how to print most events, including messages posted in groups/channels/private chats

Upvotes: 6

Jaume
Jaume

Reputation: 3780

The provided link is old but it was a good starting point. Here is the updated working code:

 while (true)
            {
                var state = await _client.SendRequestAsync<TLState>(new TLRequestGetState());
                TrackingState = state.Pts.ToString();
                var req = new TLRequestGetDifference() { Date = state.Date, Pts = state.Pts-10, Qts = state.Qts };
                var diff = await _client.SendRequestAsync<TLAbsDifference>(req);
                var msgs = diff as TLDifference;
                if (msgs!=null && msgs.NewMessages.Count>0)
                {
                    var mss = msgs.NewMessages.Where(x => x.GetType() == typeof(TLMessage))
                        .Cast<TLMessage>().ToList().Where(x => x.Date > _lastMessageStamp && x.Out == false)
                        .OrderBy(dt => dt.Date);

                    foreach (TLMessage upd in mss)
                    {
                        Console.WriteLine("New message ({0}): {1}", upd.Date, upd.Message);
                    }
                    _lastMessageStamp = mss.Any() ? mss.Max(x => x.Date) : _lastMessageStamp;
                }
                await Task.Delay(2500);
            }

Upvotes: 0

Related Questions