Бот C# на VkNet. Почему ловит не все события?
Почему у меня ловит не все события? Пробую писать сообщения в беседу с этой группой, но в консоль выводится не все.
while (true) // Бесконечный цикл, получение обновлений
{
var s = vkApi.Groups.GetLongPollServer(190776459);
var poll = vkApi.Groups.GetBotsLongPollHistory(
new BotsLongPollHistoryParams()
{ Server = s.Server, Ts = s.Ts, Key = s.Key, Wait = 25 });
if (poll.Updates == null) continue; // Проверка на новые события
foreach (var a in poll.Updates)
{
if (a.Type == GroupUpdateType.MessageNew)
{
Console.Write(a.MessageNew.Message.Text);
}
}
}
Ответы (1 шт):
Автор решения: suicia
→ Ссылка
хоть кода и больше, но попробуйте использовать это:
while (true)
{
var longPoll = _api.Groups.GetLongPollServer(_groupid);
_url = string.Format("{0}?act=a_check&key={1}&ts={2}&wait=25",
longPoll.Server,
longPoll.Key,
json != string.Empty ? JObject.Parse(json)["ts"].ToString() : longPoll.Ts
);
json = webclient.DownloadString(_url);
var msgcol = JObject.Parse(json)["updates"].ToList();
foreach (var item in msgcol)
{
if (item["type"].ToString() == "message_new")
{
string message = item["object"]["body"].ToString();
long? userid = Convert.ToInt64(item["object"]["user_id"].ToString());
SendMsg(userid, message);
}
}
}
предварительно добавив поля класса:
private static WebClient webclient = new WebClient() { Encoding = Encoding.UTF8 };
private string json = string.Empty;
private string _url = string.Empty;