当前位置: 首页>>代码示例>>C#>>正文


C# Api.GetUpdates方法代码示例

本文整理汇总了C#中Api.GetUpdates方法的典型用法代码示例。如果您正苦于以下问题:C# Api.GetUpdates方法的具体用法?C# Api.GetUpdates怎么用?C# Api.GetUpdates使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Api的用法示例。


在下文中一共展示了Api.GetUpdates方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        static async Task Run()
        {
            var Bot = new Api("Your Api Key");

            var me = await Bot.GetMe();

            Console.WriteLine("Hello my name is {0}", me.Username);

            var offset = 0;

            while (true)
            {
                var updates = await Bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    if (update.Message.Text != null)
                        await Bot.SendTextMessage(update.Message.Chat.Id, update.Message.Text);

                    offset = update.Id + 1;
                }

                await Task.Delay(1000);
            }

        }
开发者ID:lftkv,项目名称:telegram.bot,代码行数:26,代码来源:Program.cs

示例2: Run

        static async Task Run()
        {
            var Bot = new Api("Your Api Key");

            var me = await Bot.GetMe();

            Console.WriteLine("Hello my name is {0}", me.Username);

            var offset = 0;

            while (true)
            {
                var updates = await Bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    if (update.Message.Type == MessageType.TextMessage)
                    {
                        await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                        await Task.Delay(2000);
                        var t = await Bot.SendTextMessage(update.Message.Chat.Id, update.Message.Text);
                        Console.WriteLine("Echo Message: {0}", update.Message.Text);
                    }

                    if (update.Message.Type == MessageType.PhotoMessage)
                    {
                        var file = await Bot.GetFile(update.Message.Photo.LastOrDefault()?.FileId);

                        Console.WriteLine("Received Photo: {0}", file.FilePath);

                        var filename = file.FileId+"."+file.FilePath.Split('.').Last();

                        using (var profileImageStream = File.Open(filename, FileMode.Create))
                        {
                            await file.FileStream.CopyToAsync(profileImageStream);
                        }
                    }

                    offset = update.Id + 1;
                }

                await Task.Delay(1000);
            }
        }
开发者ID:carriercomm,项目名称:telegram.bot.examples,代码行数:44,代码来源:Program.cs

示例3: RunBot

        private static async Task RunBot()
        {
            InitGPIO(47);

            var Bot = new Api("Your API Key");

            var offset = 0;

            while (true)
            {
                var updates = await Bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    switch (update.Type)
                    {
                        case UpdateType.MessageUpdate:
                            var message = update.Message;

                            switch (message.Type)
                            {
                                case MessageType.TextMessage:

                                    if (message.Text == "/toggle")
                                    {
                                        ToggleLED();
                                        break;
                                    }

                                    await Bot.SendTextMessage(message.Chat.Id, message.Text,
                                        replyToMessageId: message.MessageId);

                                    break;
                            }
                            break;
                    }

                    offset = update.Id + 1;
                }
            }
        }
开发者ID:MrRoundRobin,项目名称:telegram.bot.examples,代码行数:41,代码来源:StartupTask.cs

示例4: Run

        static async Task Run()
        {
            var Bot = new Api(TokenUrl);

            var me = await Bot.GetMe();

            Console.WriteLine("Hello my name is {0}", me.Username);

            var offset = 0;

            while (true)
            {
                var updates = await Bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    if (update.Message.Type == MessageType.TextMessage)
                    {
                        await Bot.SendChatAction(update.Message.Chat.Id, ChatAction.Typing);
                        await Task.Delay(2000);

                        var splitMessage = update.Message.Text.Trim().Split(' ');

                        if (splitMessage.Length != 0 && splitMessage[0] == "budget")
                        {
                            try
                            {
                                float newBudget = float.Parse(splitMessage[1]);
                                budget = newBudget;
                                //redis.Set("budget", newBudget);
                            }
                            catch (Exception e)
                            {
                                Exception ex = e;
                            }
                        }
                        else if (splitMessage.Length != 0 && splitMessage[0] == "list")
                        {
                            var filterUser = splitMessage.Length == 2 ? splitMessage[1].Trim() : string.Empty;
                            
                            var outputString = new System.Text.StringBuilder();
                            if(s_expenses.Count == 0)
                            {
                                var tEmpty = await Bot.SendTextMessage(update.Message.Chat.Id, "No expenses recorded.");
                            }
                            else
                            {
                                foreach (var item in s_expenses)
                                {
                                    if(string.IsNullOrEmpty(filterUser) || item.User == filterUser)
                                    {
                                        var reply = string.Format("{0}: £{1} - ({2})", item.Timestamp.ToShortDateString(), item.Cost, item.Category);
                                        outputString.AppendLine(reply);
                                    }                                    
                                }

                                var t = await Bot.SendTextMessage(update.Message.Chat.Id, outputString.ToString());
                            }                            
                        }
                        else
                        {
                            try
                            {
                                var item = BudgetItem.Parse(update.Message.From.FirstName, update.Message.Text, DateTime.Now);
                                s_expenses.Add(item);
                                
                                budget -= item.Cost;
                            }
                            catch
                            {
                                var failResponse = await Bot.SendTextMessage(update.Message.Chat.Id, "Bad entry.  Try again.");
                                Console.WriteLine("Echo Message: {0}", failResponse);
                            }

                            var reply = string.Format("Budget: £{0}", budget);
                            var t = await Bot.SendTextMessage(update.Message.Chat.Id, reply);
                            Console.WriteLine("Echo Message: {0}", update.Message.Text);
                        }                        
                    }

                    if (update.Message.Type == MessageType.PhotoMessage)
                    {
                        var file = await Bot.GetFile(update.Message.Photo.LastOrDefault()?.FileId);

                        Console.WriteLine("Received Photo: {0}", file.FilePath);

                        var filename = file.FileId+"."+file.FilePath.Split('.').Last();

                        using (var profileImageStream = File.Open(filename, FileMode.Create))
                        {
                            await file.FileStream.CopyToAsync(profileImageStream);
                        }
                    }

                    offset = update.Id + 1;
                }

                await Task.Delay(1000);
            }
        }
开发者ID:mohammaddabiri,项目名称:BudgetBot,代码行数:100,代码来源:Program.cs

示例5: Run

        private static async Task Run()
        {
            if (!String.IsNullOrEmpty(Properties.Settings.Default.ProxyUser))
            {
                var proxyCreds = new NetworkCredential(
                    Properties.Settings.Default.ProxyUser,
                    Properties.Settings.Default.ProxyPassword
                );

                WebRequest.DefaultWebProxy.Credentials = proxyCreds;
            }

            var bot = new Api(Properties.Settings.Default.BotToken);
            var me = await bot.GetMe();

            Console.WriteLine("Hello my name is {0}", me.FirstName);

            var offset = 0;

            while (true)
            {
                var updates = await bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    if (update.Message.Text != null
                        && update.Message.From.Id > 0
                        // Только приватные чаты
                        && update.Message.From.Id == update.Message.Chat.Id)
                    {
                        var session = GetUserSession(update.Message.From.Id);
                        if (update.Message.Text == "/connect")
                        {
                            session.LastCommand = update.Message.Text;
                            await bot.SendTextMessage(session.UserId, "Введите имя пользователя и его пароль в две строки");
                        }
                        else if (update.Message.Text == "/disconnect")
                        {
                            SetTask(session, () => DisconnectTask(bot, session));
                            session.LastCommand = "";
                        }
                        else if (update.Message.Text == "/projects")
                        {
                            SetTask(session, () => ProjectsTask(bot, session));
                            session.LastCommand = "";
                        }
                        else if (update.Message.Text == "/object")
                        {
                            SetTask(session, () => ObjectTask(bot, session));                            
                            session.LastCommand = update.Message.Text;
                        }
                        else if (update.Message.Text == "/file")
                        {
                            SetTask(session, () => FileTask(bot, session));
                            session.LastCommand = update.Message.Text;
                        }
                        else if (update.Message.Text == "/subscribe")
                        {
                            session.LastCommand = "";
                        }
                        else if (update.Message.Text == "/start" || update.Message.Text == "/help")
                        {
                            session.LastCommand = "";
                        }
                        else if (session.LastCommand == "/connect")
                        {
                            var cred = update.Message.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                            if (cred.Count() != 2)
                                await bot.SendTextMessage(session.UserId, "Введите имя пользователя и его пароль в две строки");
                            else
                            {
                                SetTask(session, () => ConnectTask(bot, session, cred[0], cred[1]));
                                session.LastCommand = "";
                            }
                        }
                        else if (session.LastCommand == "/object")
                        {
                            SetTask(session, () => ObjectTask(bot, session, update.Message.Text));
                            session.LastCommand = "";
                        }
                        else if (session.LastCommand == "/file")
                        {
                            SetTask(session, () => FileTask(bot, session, update.Message.Text));
                            session.LastCommand = "";
                        }
                        else
                        {
                            session.LastCommand = "";
                            await bot.SendTextMessage(session.UserId, "Неизвестная команда");
                        }
                    }

                    offset = update.Id + 1;
                }

                await Task.Delay(1000);
            }
        }
开发者ID:select-artur,项目名称:Telegram.Bot.Loodsman,代码行数:98,代码来源:Program.cs

示例6: Run

        static async Task Run() 
        //static async void Run()
        {//Change
            var Bot = new Api("139410021:AAHoQsUYiH_NiHV0vutnDWSLy1JEslIibz0");

            var me = await Bot.GetMe();

            Console.WriteLine("Hello my name is {0}", me.Username);

            var offset = 0;

            //SQLiteConnection con = new SQLiteConnection();
            //SQLiteCommand com = new SQLiteCommand();
            SqlConnection con = new SqlConnection();
            SqlCommand com = new SqlCommand();
            int userId, courseId, messageId = 0;
            while (true)
            {
                messageId = (int)DataAccessSql.executeScalar(new SqlCommand("SELECT MAX(MESSAGEID) FROM TelegramUpdate"));
                var updates = await Bot.GetUpdates(offset: 0);
               
                Telegram.Bot.Types.Update upd;
                upd = new Telegram.Bot.Types.Update();
                com.Connection = con;
                //con.ConnectionString = @"data source=C:\Users\Administrator\Documents\Visual Studio 2015\Projects\telegram.bot\Telegram.Bot.Echo\db\telegramcontacts.sqlite;
                //   Pooling=true;FailIfMissing=false;Version=3";
                con.ConnectionString = returnConnection(1);
                com.CommandText = @"INSERT INTO TelegramUpdate (Id, messageText, ChatId, ChatFirstName, ChatLastName, ChatUserName, Date, FromFirstName,FromLastName, FromUserName, FromId, MessageId, MessageType)
                                    VALUES(@Id, @messageText, @ChatId, @ChatFirstName, @ChatLastName, @ChatUserName, @Date, @FromFirstName, @FromLastName, @FromUserName, @FromId, @MessageId, @MessageType)";
                //,(botUpdate.Id,botUpdate.message.Text,botUpdate.message.chat.Id,botUpdate.chat.first_name,botUpdate.chat.Laset_name,)";

                foreach (var update in updates)
                {
                    upd = update;
                    if (upd.Message.MessageId > messageId)
                    {
                        try
                        {
                            com.Parameters.Clear();
                            com.Parameters.AddWithValue("Id", upd.Id);
                            com.Parameters.AddWithValue("messageText", upd.Message.Text);
                            com.Parameters.AddWithValue("ChatId", upd.Message.Chat.Id);
                            com.Parameters.AddWithValue("ChatFirstName", upd.Message.Chat.FirstName);
                            com.Parameters.AddWithValue("ChatLastName", "upd.Message.Chat.LastName");
                            com.Parameters.AddWithValue("ChatUserName", upd.Message.Chat.FirstName);
                            com.Parameters.AddWithValue("Date", upd.Message.Date);
                            com.Parameters.AddWithValue("FromFirstName", upd.Message.From.FirstName);
                            com.Parameters.AddWithValue("FromLastName", "upd.Message.From.LastName");
                            com.Parameters.AddWithValue("FromUserName", "upd.Message.From.Username");
                            com.Parameters.AddWithValue("FromId", upd.Message.From.Id);
                            com.Parameters.AddWithValue("MessageId", upd.Message.MessageId);
                            com.Parameters.AddWithValue("MessageType", upd.Message.Type.ToString());
                            com.Connection.Open();
                            com.ExecuteNonQuery();
                            messageId = upd.Message.MessageId;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + ' ' + System.DateTime.Now.ToString());
                        }
                        finally
                        {
                            com.Connection.Close();
                        }
                    }

                    if (update.Message.Type == MessageType.PhotoMessage)
                    {
                        var file = await Bot.GetFile(update.Message.Photo.LastOrDefault()?.FileId);

                        Console.WriteLine("Received Photo: {0}", file.FilePath);

                        var filename = file.FileId + "." + file.FilePath.Split('.').Last();

                        using (var profileImageStream = File.Open(filename, FileMode.Create))
                        {
                            await file.FileStream.CopyToAsync(profileImageStream);
                        }
                    }

                    offset = update.Id + 1;
                }

                //await Task.Delay(1000);
                offset++;
            }
        }
开发者ID:arghand,项目名称:TelegramBot,代码行数:87,代码来源:Program.cs


注:本文中的Api.GetUpdates方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。