Start friendship with VkNet

  • Tutorial
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.

VkApiapi = newVkApi();


Now you need to log in.

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

Get a list of the last 20 conversations

vardialogs = api.Messages.GetConversations(newGetConversationsParams());

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

varmessages = api.Messages.GetHistory(newMessagesGetHistoryParams()
                {PeerId = dialogs.Items[0].Conversation.Peer.Id});

And, perhaps, we will print the results.

foreach (varmsginmessages.Messages)
            {
                Console.WriteLine(msg.Text);
            }

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

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



As an example:



And we received such a final code.

here
VkApiapi = newVkApi(); 
            api.Authorize(newApiAuthParams()
            {
                Login = "*", 
                Password = "*",
                ApplicationId = *,
                Settings = Settings.All
            });
            vardialogs = api.Messages.GetConversations(newGetConversationsParams());
            varmessages = api.Messages.GetHistory(newMessagesGetHistoryParams()
                {PeerId = dialogs.Items[0].Conversation.Peer.Id});
            Console.OutputEncoding = Encoding.UTF8;
            foreach (varmsginmessages.Messages)
            {
                Console.WriteLine(msg.Text);
            }
            api.Messages.Send(newMessagesSendParams()
            {
                PeerId = messages.Messages.First().Id,
                Message = "Test",
                RandomId = new Random().Next()
            });


PS

Support can be found here .

Also popular now: