Advertisement
AvesShch

Untitled

Jul 16th, 2020
1,369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using FeedbackBots;
  2. using FeedbackBots.Data;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using VkNet;
  9. using VkNet.Enums.Filters;
  10. using VkNet.Enums.SafetyEnums;
  11. using VkNet.Model;
  12. using VkNet.Model.GroupUpdate;
  13. using VkNet.Model.RequestParams;
  14.  
  15. namespace VkBot
  16. {
  17.     public class VkBotBody : IBotListener
  18.     {
  19.         private readonly IBotLogic bot;
  20.         private readonly VkApi api;
  21.         private readonly IGroupNotificationBotRepository repository;
  22.  
  23.  
  24.         public VkBotBody(IBotLogic bot, IGroupNotificationBotRepository repository)
  25.         {
  26.             api = new VkApi();
  27.             api.Authorize(new ApiAuthParams { AccessToken = Environment.GetEnvironmentVariable("AccessToken"), Settings = Settings.Messages});
  28.             this.bot = bot;
  29.             bot.Subscribe(this);
  30.             this.repository = repository;
  31.         }
  32.  
  33.         public void Start()
  34.         {
  35.             Console.WriteLine($"Starting {bot.GetType()}-bot");
  36.             new Thread(Run).Start();
  37.             Console.WriteLine($"{bot.GetType()}-bot started");
  38.         }
  39.  
  40.         private void Run()
  41.         {
  42.             var sessionData = GetSessionData();
  43.             var ts = sessionData.Ts;
  44.             while (true)
  45.             {
  46.                 var history = GetBotHistory(sessionData, ts);
  47.                 ts = history.Ts;
  48.  
  49.                 ProcessUpdates(history.Updates);
  50.  
  51.             }
  52.             // ReSharper disable once FunctionNeverReturns
  53.         }
  54.  
  55.         private LongPollServerResponse GetSessionData()
  56.         {
  57.             return api.Groups.GetLongPollServer(ulong.Parse(Environment.GetEnvironmentVariable("GroupId")));
  58.         }
  59.  
  60.  
  61.         private BotsLongPollHistoryResponse GetBotHistory(LongPollServerResponse sessionData, string ts)
  62.         {
  63.             return api.Groups.GetBotsLongPollHistory(new BotsLongPollHistoryParams
  64.             {
  65.                 Server = sessionData.Server,
  66.                 Key = sessionData.Key,
  67.                 Ts = ts,
  68.                 Wait = 25
  69.             });
  70.         }
  71.  
  72.         private void ProcessUpdates(IEnumerable<GroupUpdate> updates)
  73.         {
  74.             foreach (var update in updates)
  75.             {
  76.                 if (update.Type == GroupUpdateType.MessageNew)
  77.                 {
  78.                     var peerId = update.Message.PeerId;
  79.                     var message = DeleteReference(update.Message.Text);
  80.                     if (update.Message.FromId == peerId || peerId == null)
  81.                         continue;
  82.  
  83.                     var reply = bot.Reply(new BotUser((long)peerId), message);
  84.                     Listen((long)peerId, reply);
  85.                 }
  86.                 else if (update.Type == GroupUpdateType.WallPostNew)
  87.                 {
  88.                     SendToAll(update.WallPost.Text);
  89.                 }
  90.             }
  91.         }
  92.  
  93.         private static string DeleteReference(string rawTextMessage)
  94.         {
  95.             return Regex.Replace(rawTextMessage, @"^\[.*\],*\s*", "");
  96.         }
  97.  
  98.         private void SendMessage(long? peerId, string text)
  99.         {
  100.             api.Messages.Send(new MessagesSendParams
  101.             {
  102.                 RandomId = new DateTime().Millisecond,
  103.                 PeerId = peerId,
  104.                 Message = text
  105.             });
  106.         }
  107.  
  108.         public void Listen(long id, BotReply reply)
  109.         {
  110.             SendMessage(id, reply.Text);
  111.         }
  112.         public long GetNumberOfMembersInGroup(AcademicGroup group)
  113.         {
  114.             return group.PeerIds.Where(peerId => peerId != null)
  115.                 .Select(peerId => api.Messages.GetConversationMembers((long)peerId, new List<string>()))
  116.                 .Select(a => a.Count)
  117.                 .Sum();
  118.         }
  119.  
  120.         private void SendToAll(string message)
  121.         {
  122.             foreach (var id in repository.GetGroupsList().SelectMany(g => g.PeerIds))
  123.             {
  124.                 if (id != null)
  125.                     bot.Send((long)id, new BotReply($"Новая запись в группе:\n{message}"));
  126.             }
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement