cactucatu4eckuu / vknetextend Goto Github PK
View Code? Open in Web Editor NEWРасширение для библиотеки VkNet
Расширение для библиотеки VkNet
Если отправить боту сообщение, то он начинает его бесконечно отправлять, хотя это не подразумевается
Скриншот
class MainClass
{
private static VkApi api = new VkApi();
public static void Init(string token)
{
ApiAuthParams authParams = new ApiAuthParams();
authParams.AccessToken = token;
api.Authorize(authParams);
if (api.IsAuthorized)
Task.Run(() => botStarted());
}
private static void botStarted()
{
MessageBox.Show("started", "started", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageLongPollWatcherOptions options = new MessageLongPollWatcherOptions();
VkNetExtMessageLongPollWatcher watcher = new VkNetExtMessageLongPollWatcher(options, api);
watcher.NewMessages += newMessage;
watcher.StartWatchAsync();
}
private static void newMessage(IMessageLongPollWatcher watcher, IEnumerable<Message> messages)
{
var messagesList = messages.ToArray();
if (messagesList.Length < 1) return;
//MessageBox.Show($"message count:{messagesList.Count}", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
for (int i = 0; i < messagesList.Length; i++)
{
var message = messagesList[i];
string text = message.Text;
long? peerId = message.PeerId != null ? message.PeerId : message.PeerId;
long? userId = message.UserId != null ? message.UserId : message.PeerId;
if (peerId == api.UserId) continue;
if(peerId != userId)
{
}
else
{
MessagesSendParams messageSend = new MessagesSendParams();
messageSend.PeerId = userId;
messageSend.Message = text;
messageSend.RandomId = Environment.TickCount;
api.Messages.Send(messageSend);
}
}
}
}
protected async Task WatchStepAsync(object state)
{
if (Active)
{
var history = await GetLongPollHistoryAsync().ConfigureAwait(true);
if (history != null)
{
if (history.History.Count > 0)
{
CurrentSleepSteps = 1;
NewEvents?.Invoke(this, history);
if (history.Messages.Count > 0)
NewMessages?.Invoke(this, history.Messages);
}
else
{
if (CurrentSleepSteps < _options.MaxSleepSteps)
CurrentSleepSteps++;
}
_watchTimer?.Change(CurrentSleepSteps * _options.StepSleepTimeMsec, Timeout.Infinite);
}
else
{
// TODO: Define situation when exceptions occured
_watchTimer?.Change(CurrentSleepSteps * _options.StepSleepTimeMsec, Timeout.Infinite);
}
}
}
private void _watchStep(object state) => WatchStepAsync(state).GetAwaiter().GetResult();// исключение в этой строке System.NullReferenceException
Мой код
void server()
{
var PollServer = api.Messages.GetLongPollServer(true);
VkNetExtend.MessageLongPoll.MessageLongPollWatcherOptions longPollWatcherOptions = new VkNetExtend.MessageLongPoll.MessageLongPollWatcherOptions();
VkNetExtend.MessageLongPoll.VkNetExtMessageLongPollWatcher Watcher = new VkNetExtend.MessageLongPoll.VkNetExtMessageLongPollWatcher(longPollWatcherOptions, api);
VkNetExtend.MessageLongPoll.Models.StartWatchModel startWatchModel = new VkNetExtend.MessageLongPoll.Models.StartWatchModel();
Watcher.StartWatchAsync(startWatchModel);
Watcher.NewMessages += Watcher_NewMessages;
}
private void Watcher_NewMessages(VkNetExtend.MessageLongPoll.IMessageLongPollWatcher watcher,IEnumerable<VkNet.Model.Message> messages)
{
MessageBox.Show("Новое сообщение!");
var message = messages.ToArray();
for (int i = 0; i < message.Length; i++)
{
commandReact(message[i].Body, message[i].FromId);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.