📜 ⬆️ ⬇️

Start friendship with VkNet

I am friends with the VkNet library a lot . But, unfortunately, the documentation for working with it is quite outdated. So I decided to share and create a small tutorial on the basics of working with this library.

First you need to create an application here .



And from the settings of the newly created application, take App Id.



Now you can go directly to the code.

Create a console .net core application and add the VkNet dependency.



Create an instance of VkApi.

VkApi api = new VkApi(); 


Now you need to log in.

 api.Authorize(new ApiAuthParams() { Login = "+79*******", Password = "***", ApplicationId = 684***, Settings = Settings.All }); 

Get a list of the last 20 conversations

 var dialogs = api.Messages.GetConversations(new GetConversationsParams()); 

And now we get the last 20 messages from the first dialogue.

 var messages = api.Messages.GetHistory(new MessagesGetHistoryParams() {PeerId = dialogs.Items[0].Conversation.Peer.Id}); 

And, perhaps, we will print the results.

 foreach (var msg in messages.Messages) { Console.WriteLine(msg.Text); } 

And send a test message to the author of the first previous message.

 api.Messages.Send(new MessagesSendParams() { // ну или подставить сюда ид из диалога PeerId = messages.Messages.First().Id, Message = "Test", RandomId = new Random().Next() }); 



As an example:



And we received such final code

here
 VkApi api = new VkApi(); api.Authorize(new ApiAuthParams() { Login = "*", Password = "*", ApplicationId = *, Settings = Settings.All }); var dialogs = api.Messages.GetConversations(new GetConversationsParams()); var messages = api.Messages.GetHistory(new MessagesGetHistoryParams() {PeerId = dialogs.Items[0].Conversation.Peer.Id}); Console.OutputEncoding = Encoding.UTF8; foreach (var msg in messages.Messages) { Console.WriteLine(msg.Text); } api.Messages.Send(new MessagesSendParams() { PeerId = messages.Messages.First().Id, Message = "Test", RandomId = new Random().Next() }); 


PS

Support can be found here .

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