Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using FeedbackBots;
- using FeedbackBots.Data;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Threading;
- using VkNet;
- using VkNet.Enums.Filters;
- using VkNet.Enums.SafetyEnums;
- using VkNet.Model;
- using VkNet.Model.GroupUpdate;
- using VkNet.Model.RequestParams;
- namespace VkBot
- {
- public class VkBotBody : IBotListener
- {
- private readonly IBotLogic bot;
- private readonly VkApi api;
- private readonly IGroupNotificationBotRepository repository;
- public VkBotBody(IBotLogic bot, IGroupNotificationBotRepository repository)
- {
- api = new VkApi();
- api.Authorize(new ApiAuthParams { AccessToken = Environment.GetEnvironmentVariable("AccessToken"), Settings = Settings.Messages});
- this.bot = bot;
- bot.Subscribe(this);
- this.repository = repository;
- }
- public void Start()
- {
- Console.WriteLine($"Starting {bot.GetType()}-bot");
- new Thread(Run).Start();
- Console.WriteLine($"{bot.GetType()}-bot started");
- }
- private void Run()
- {
- var sessionData = GetSessionData();
- var ts = sessionData.Ts;
- while (true)
- {
- var history = GetBotHistory(sessionData, ts);
- ts = history.Ts;
- ProcessUpdates(history.Updates);
- }
- // ReSharper disable once FunctionNeverReturns
- }
- private LongPollServerResponse GetSessionData()
- {
- return api.Groups.GetLongPollServer(ulong.Parse(Environment.GetEnvironmentVariable("GroupId")));
- }
- private BotsLongPollHistoryResponse GetBotHistory(LongPollServerResponse sessionData, string ts)
- {
- return api.Groups.GetBotsLongPollHistory(new BotsLongPollHistoryParams
- {
- Server = sessionData.Server,
- Key = sessionData.Key,
- Ts = ts,
- Wait = 25
- });
- }
- private void ProcessUpdates(IEnumerable<GroupUpdate> updates)
- {
- foreach (var update in updates)
- {
- if (update.Type == GroupUpdateType.MessageNew)
- {
- var peerId = update.Message.PeerId;
- var message = DeleteReference(update.Message.Text);
- if (update.Message.FromId == peerId || peerId == null)
- continue;
- var reply = bot.Reply(new BotUser((long)peerId), message);
- Listen((long)peerId, reply);
- }
- else if (update.Type == GroupUpdateType.WallPostNew)
- {
- SendToAll(update.WallPost.Text);
- }
- }
- }
- private static string DeleteReference(string rawTextMessage)
- {
- return Regex.Replace(rawTextMessage, @"^\[.*\],*\s*", "");
- }
- private void SendMessage(long? peerId, string text)
- {
- api.Messages.Send(new MessagesSendParams
- {
- RandomId = new DateTime().Millisecond,
- PeerId = peerId,
- Message = text
- });
- }
- public void Listen(long id, BotReply reply)
- {
- SendMessage(id, reply.Text);
- }
- public long GetNumberOfMembersInGroup(AcademicGroup group)
- {
- return group.PeerIds.Where(peerId => peerId != null)
- .Select(peerId => api.Messages.GetConversationMembers((long)peerId, new List<string>()))
- .Select(a => a.Count)
- .Sum();
- }
- private void SendToAll(string message)
- {
- foreach (var id in repository.GetGroupsList().SelectMany(g => g.PeerIds))
- {
- if (id != null)
- bot.Send((long)id, new BotReply($"Новая запись в группе:\n{message}"));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement