⬆️ ⬇️

One VC bot, one C # and an orange

Continuing my experiments with a “smart” home, for fun, I decided to add a group to VK to control some of its characteristics. For this article, we specify the task: we will try to write in the language of a simple bot, which will respond on behalf of the community in VK, and consider how quickly it can be run on arm32 (in my case, orange pi zero).







Thought about the warmth
There are many other options (for example, runtime), this is one of them



So, decompose on shelves.



Create an application in VC
Here is the detailed documentation.

  1. To create a bot go here

    Click "create application" and select "Standalone Application".
  2. Now we are moving to the management and in the Application Id tab we remember its Id. It is useful to us further.




We get a token for working with groups
  1. We send the request by simply inserting into the browser string:

    https://oauth.vk.com/authorize?client_id=YOURAPPID&group_ids=YOURGROUPID6&display=page&scope=messages,wall,manage&response_type=token&v=5.92 


    where YOURAPPID is the application id that we found in the previous spoiler, and YOURGROUPID id is your community.

  2. We give access to the application
  3. And we get this answer

     https://oauth.vk.com/blank.html#expires_in=0&access_token_YOURGROUPID=YOURTOKEN 


    Where the token will be a very long combination of Latin letters and numbers





Easier we get the token
  1. Go to the community management




Set up a community to work with long poll
  1. Go to the management tab of our community.
  2. Api Usage and LongPoll Api
  3. Event types (events), we mark the necessary ones in them, for tests I would note everything.




Go to the main part:

')

We start favorite ide, we create the console application on net core







Add VkNet



spoiler
Unfortunately on the wiki, the documentation is a bit outdated. One of the reasons for creating this guide.

But there is great support here.





Log in using our token:



 var api = new VkApi(); api.Authorize(new ApiAuthParams(){AccessToken =MyAppToken }); 


And in an endless loop we will receive updates.



  while (true) { var s = api.Groups.GetLongPollServer(MyGroupId); var poll = api.Groups.GetBotsLongPollHistory( new BotsLongPollHistoryParams() {Server = s.Server, Ts = s.Ts, Key = s.Key, Wait = 1}); } 


Check if something has come to us



 if(poll?.Updates== null) continue; 


For all received data, we will find out if any of this is a message, if so, then we will print its contents.



  foreach (var a in poll.Updates) { if (a.Type == GroupUpdateType.MessageNew) { Console.WriteLine(a.Message.Body); } } 


And answer the user with the same text



  api.Messages.Send(new MessagesSendParams() { UserId = a.Message.UserId, Message = a.Message.Body }); 




Received code
 class Program { public static string MyAppToken => "f6bf5e26*************************************************************"; public static ulong MyGroupId => 10******; static void Main(string[] args) { var api = new VkApi(); api.Authorize(new ApiAuthParams(){AccessToken =MyAppToken }); while (true) { var s = api.Groups.GetLongPollServer(MyGroupId); var poll = api.Groups.GetBotsLongPollHistory( new BotsLongPollHistoryParams() {Server = s.Server, Ts = s.Ts, Key = s.Key, Wait = 1}); if(poll?.Updates== null) continue; foreach (var a in poll.Updates) { if (a.Type == GroupUpdateType.MessageNew) { Console.WriteLine(a.Message.Body); api.Messages.Send(new MessagesSendParams() { UserId = a.Message.UserId, Message = a.Message.Body }); } } } } } 




Collect the resulting code for our board



 dotnet publish . -r linux-arm 


And we drag the directory to the board







Go through ssh and run



 chmod +x ConsoleApp1 ./ConsoleApp1 


Result
We send the message







We receive the message in the console







We get the answer







Dialogue





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



All Articles