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


C# Api.SendChatAction方法代码示例

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


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

示例2: 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


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