Beispiel #1
0
        public static Update[] getFirstUpdates(int to = 1800)
        {
            SortedDictionary <long, Update> updates = new SortedDictionary <long, Update>();
            Result <Update[]> result = null;

            if (Configs.webhookinfo.pending_update_count == 0)
            {
                Configs.webhookinfo.pending_update_count = 1;
            }                                                                                                    //make it wait for first post anyways

            while (updates.Count() < Configs.webhookinfo.pending_update_count)
            {
                GetUpdates u = new GetUpdates()
                {
                    limit = 100, timeout = to
                };
                if (MainClass.UpdateId > 0)
                {
                    u.offset = MainClass.UpdateId + 1;
                }

                result = sendRequest <Update[]>(Method.getUpdates, buildRequest <GetUpdates>(u));

                if (result == null || !result.ok || result.result.Length < 1)
                {
                    if (MainClass.UpdateId > 0)
                    {
                        return(null);
                    }
                    else
                    {
                        Thread.Sleep(1000);
                        continue;
                    }
                }

                foreach (Update update in result.result)
                {
                    if (updates.ContainsKey(update.update_id))
                    {
                        continue;
                    }
                    updates.Add(update.update_id, update);
                    if (update.update_id > MainClass.UpdateId)
                    {
                        MainClass.UpdateId = update.update_id;
                    }
                }
            }

            return(updates.Values.ToArray());
        }
Beispiel #2
0
        internal static Result <Update[]> getUpdates(GetUpdates args = null)
        {
            Result <Update[]> result = null;

            if (args == null)
            {
                result = sendRequest <Update[]>(Method.getUpdates);
            }
            else
            {
                result = sendRequest <Update[]>(Method.getUpdates, buildRequest <GetUpdates>(args));
            }
            return(result);
        }
Beispiel #3
0
        static void Main()
        {
            #region Bot Initialization Phase

            AppDomain.CurrentDomain.ProcessExit += new EventHandler(ExitCleanUp);

            Database.Init();

            Result <User> res = null;
            while (res == null)
            {
                res = Methods.getMe();
                if (res == null)
                {
                    Logger.LogError("Error getting bot info. Reattempting..");
                }
                else if (!res.ok)
                {
                    Logger.LogFatal("Error getting bot info (" + res.errorCode + ") " + res.description);
                    Logger.LogFatal("Press anykey to terminate...");
                    Console.ReadKey();
                    Environment.Exit(-1);
                }
            }

            Configs.Me = res.result;
            Result <WebhookInfo> webres = Methods.getWebhookInfo();
            if (!webres.ok)
            {
                Logger.LogFatal("Error getting bot info: " + webres.description);
                Environment.Exit(-1);
            }
            Configs.webhookinfo = webres.result;

            ChatCaching.Init();

            PluginManager.Init();

            Cron.CronInit();

            if (Configs.RunningConfig.Owner == 0)
            {
                Console.WriteLine("There is no owner assigned to this bot.\r\nPlease send me the command below to claim ownership.\r\nThis token will be deleted once you use it.\r\n\r\n");
                Console.WriteLine("/token " + Utilities.CreateAdminToken(true));
            }

            if (Configs.RunningConfig.AdminChat < 0 || Configs.RunningConfig.AdminChat > 0)
            {
                string text = "**DreadBot Started**\n" + Utilities.NormalTime(Utilities.EpochTime()) + "\n\n" + PluginManager.getPluginList();
                Methods.sendMessage(Configs.RunningConfig.AdminChat, text);
            }

            Console.Title = "DreadBot v" + Configs.Version + " @" + Configs.Me.username;
            Console.WriteLine(PluginManager.getPluginList() + "\n");
            Console.WriteLine("DreadBot Loaded, and Started!\n");

            if (!String.IsNullOrEmpty(Configs.webhookinfo.url))
            {
                Configs.RunningConfig.GetupdatesMode = false;
            }                                                                                                     //WebHook is enabled. Launch in Webhook mode.

            #endregion

            while (true)
            {
                #region Main GetUpdates Loop

                if (Configs.RunningConfig.GetupdatesMode) //GetUpdates Mode
                {
                    Update[] updates = null;
                    if (UpdateId == 0)
                    {
                        if (Configs.webhookinfo.pending_update_count > 3)
                        {
                            Logger.LogInfo("Playing catchup; " + Configs.webhookinfo.pending_update_count + " updates behind.");
                        }
                        updates = Methods.getFirstUpdates(60);
                        if (updates == null || updates.Length < 1)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        GetUpdates request = new GetUpdates()
                        {
                            timeout = 20,
                            offset  = UpdateId + 1,
                            limit   = Configs.RunningConfig.GULimit
                        };
                        Result <Update[]> updatesres = Methods.getUpdates(request);
                        if (updatesres == null || !updatesres.ok)
                        {
                            Logger.LogError("Error fetching updates: (" + updatesres.errorCode + ") " + updatesres.description);
                            Thread.Sleep(10000);
                            continue;
                        }
                        updates = updatesres.result;
                    }
                    if (updates.Length < 1)
                    {
                        continue;
                    }
                    foreach (Update update in updates)
                    {
                        UpdateId = update.update_id;
                        //Console.WriteLine("Parsing Update: " + UpdateId);
                        Events.ParseUpdate(update);
                    }
                }

                #endregion

                #region WebHook Loop

                else // Webhook mode
                {
                    //// Webhook Not Implemented.
                    Logger.LogError("DreadBot Cannot continue:\n\nWebhook Mode is enabled for this bot. This version of DreadBot does not have Webhook support and will close.\n\n");
                    ExitCleanUp(null, null);
                    Thread.Sleep(10000);
                    throw new NotImplementedException("This version of DreadBot does not have Webhook support and will terminate.");
                }

                #endregion
            }
        }