⬆️ ⬇️

Uploading messages using the Telegram API. Where to begin

On the Internet, you can find many descriptions and examples on the creation of telegram bot. But Telegram Bot Api has limited functionality. To solve the problem of unloading messages from Telegram for a keyword, you need to use the Telegram API. The basis was taken TLSharp library. It is necessary to immediately clarify that further work is possible only for registered users in Telegram.



The start of working with the API is the registration of the application:



  1. We go to the site Telegram and enter your phone number. Enter the confirmation code.

    ')

    image
  2. We get to the developer page. To complete the registration, you must fill in the fields describing your future application.



    image
  3. We get api_id and api_hash.


Work with the library



Add a Visual Studio project via the package manager console:



Install-Package TLSharp 


To get started, we create a client with the api_id and api_hash parameters obtained above - make a connection.



 TelegramClient client = new TelegramClient(apiId, apiHash); await client.ConnectAsync(); 


To authenticate your account, do the following:



 var hash = await client.SendCodeRequestAsync(" "); var code = " "; var user = await client.MakeAuthAsync(" ", hash, code); 


After completing this step, TLSharp creates the session.dat file, which stores all the user session information. After that, you can perform any request presented in the documentation .



We start the unloading of messages from the channels, a prerequisite, you must first be subscribed to the channel. We get all the dialogues that the user has:



 var dialogs = (TLDialogs)await client.GetUserDialogsAsync(); 


The recorded variable now stores all the dialogs, but in order to select only the channels, the convenient structure allows you to select the dialogs of channels and chats using:



 dialogs.Chats 


We pass through all chats and channels, separately checking whether the following element is a chat or a channel:



 foreach (var element in dialogs.Chats){ if (element is TLChat){ TLChat chat = element as TLChat; 


The library TLSharp implemented all the functions of the official API . Functions are implemented through requests, for example, additional information on the channel can be obtained through a request of the form:



 TeleSharp.TL.Messages.TLChatFull channelInfo = await client.SendRequestAsync<TeleSharp.TL.Messages.TLChatFull> (new TLRequestGetFullChat(){ChatId = chat.Id}); 


The SendRequestAsync <TeleSharp.TL.Messages.TLChatFull> method is called with the return type and any of the queries in the library follows as a parameter. The following is the code that receives complete information about the channel, in this case, to get the channel, two parameters are needed - these are ChannelId and AccessHash:



 if (element is TLChannel){ var offset = 0; TLChannel channel = element as TLChannel; var chan = await client.SendRequestAsync<TeleSharp.TL.Messages.TLChatFull>(new TLRequestGetFullChannel() { Channel = new TLInputChannel() { ChannelId = channel.Id, AccessHash = (long)channel.AccessHash} }); TLInputPeerChannel inputPeer = new TLInputPeerChannel() { ChannelId = channel.Id, AccessHash = (long)channel.AccessHash }; 


Uploading messages



Messages are unloaded in portions of 100 messages, and the AddOffset parameter configures the unloading of the next message batch.



 while (true){ TLChannelMessages res = await client.SendRequestAsync<TLChannelMessages> (new TLRequestGetHistory(){ Peer = inputPeer, Limit = 400, AddOffset = offset, OffsetId = 0 }); var msgs = res.Messages; 


Messages are of two types TLMessage and TLMessageService. The first is a plain text message, possibly containing some media file. The second type of message in most cases is a call or messages from the Telegram Service.



 if (res.Count > offset){ offset += msgs.Count; foreach (var msg in msgs) { if (msg is TLMessage) { TLMessage sms = msg as TLMessage; st.Add(sms.Message); } if (msg is TLMessageService) continue; } } else break; } } 


Search by posts



Add a preliminary search for messages in the channel. To do this, simply use the same query, the structure of which is presented above.



 TLInputPeerChannel inputPeer = new TLInputPeerChannel() { ChannelId = channel.Id, AccessHash = (long)channel.AccessHash}; TLChannelMessages search = await client.SendRequestAsync<TLChannelMessages> (new TeleSharp.TL.Messages.TLRequestSearch(){ Peer = inputPeer, MaxDate = maxdate, MinDate = mindate, Q = searchWord, Filter = new TLInputMessagesFilterEmpty(), Limit = 100, Offset = offset }); 


To form your request, you just have to fill in the parameters and then process the received messages in the same way.



That's all, I hope someone this article was helpful.

Source: https://habr.com/ru/post/351350/



All Articles