Пример #1
0
 public async void SendSingleInlineQueryAnswer(string queryId, InlineQueryResultBase baseResult)
 {
     try
     {
         await BotClient.AnswerInlineQueryAsync(queryId, new[] { baseResult });
     }
     catch (Telegram.Bot.Exceptions.BadRequestException e)
     {
         Logger.Warn(e, "Can't send single inline query answer");
     }
 }
Пример #2
0
        private async void BotClient_OnInlineQuery(Object sender, Telegram.Bot.Args.InlineQueryEventArgs e)
        {
            List <InlineQueryResultBase> list = new List <InlineQueryResultBase>();

            DataBase db = Singleton.GetInstance().Context;

            Channel[]  channels   = db.GetChannels();
            Category[] categories = db.GetCategories();

            if (e.InlineQuery.Query == "")
            {
                list.Clear();

                foreach (Channel channel in channels)
                {
                    Settings settings = db._settings.FirstOrDefault();
                    if (channel.IDChannel != settings.ChannelAdmin)
                    {
                        String text = "";
                        if (channel.Description != null)
                        {
                            List <String> description = channel.Description.Split("\n").ToList();
                            for (Int32 i = 0; i < description.Count; i++)
                            {
                                if (description[i].StartsWith("ПРАВИЛА"))
                                {
                                    continue;
                                }

                                if (description[i].Length == 0)
                                {
                                    continue;
                                }

                                text += description[i] + "\n" + "\n";
                            }
                        }

                        List <List <InlineKeyboardButton> > button = new List <List <InlineKeyboardButton> >
                        {
                            new List <InlineKeyboardButton>()
                        };
                        button[button.Count - 1]
                        .Add(new InlineKeyboardButton()
                        {
                            Text = "Перейти в канал", Url = channel.LinkChannel
                        });
                        button.Add(new List <InlineKeyboardButton>());
                        button[button.Count - 1]
                        .Add(new InlineKeyboardButton()
                        {
                            Text = "Категории", SwitchInlineQueryCurrentChat = ""
                        });
                        button.Add(new List <InlineKeyboardButton>());
                        button[button.Count - 1]
                        .Add(new InlineKeyboardButton()
                        {
                            Text = "Закрыть", CallbackData = CommandText.Close
                        });

                        list.Add(new InlineQueryResultArticle(id: channel.IDChannel.ToString(),
                                                              title: channel.ChannelName,
                                                              inputMessageContent:
                                                              new
                                                              InputTextMessageContent($"{text}\nЗаходите - " +
                                                                                      channel.InviteLink))
                        {
                            Url         = "https://" + channel.LinkChannel,
                            Title       = channel.ChannelName,
                            Description = text,
                            HideUrl     = true,
                            ThumbUrl    = channel.PhotoLink,
                            ThumbHeight = 48,
                            ThumbWidth  = 48,
                            ReplyMarkup = button.ToArray()
                        });
                    }
                }
            }

            else if (categories.Any(p => p.Name == e.InlineQuery.Query))
            {
                list.Clear();
                Category category = categories.FirstOrDefault(p => p.Name == e.InlineQuery.Query);
                foreach (Channel channel in channels)
                {
                    if (category.Id != channel.CategoryId)
                    {
                        continue;
                    }

                    String text = "";
                    if (channel.Description != null)
                    {
                        List <String> description = channel.Description.Split("\n").ToList();
                        for (Int32 i = 0; i < description.Count; i++)
                        {
                            if (description[i].StartsWith("ПРАВИЛА"))
                            {
                                continue;
                            }

                            if (description[i].Length == 0)
                            {
                                continue;
                            }

                            text += description[i] + "\n" + "\n";
                        }
                    }

                    List <List <InlineKeyboardButton> > button = new List <List <InlineKeyboardButton> >
                    {
                        new List <InlineKeyboardButton>()
                    };
                    button[button.Count - 1]
                    .Add(new InlineKeyboardButton()
                    {
                        Text = "Перейти в канал", Url = channel.LinkChannel
                    });
                    button.Add(new List <InlineKeyboardButton>());
                    button[button.Count - 1]
                    .Add(new InlineKeyboardButton()
                    {
                        Text = "Категории", SwitchInlineQueryCurrentChat = ""
                    });
                    button.Add(new List <InlineKeyboardButton>());
                    button[button.Count - 1]
                    .Add(new InlineKeyboardButton()
                    {
                        Text = "Закрыть", CallbackData = CommandText.Close
                    });

                    list.Add(new InlineQueryResultArticle(id: channel.IDChannel.ToString(),
                                                          title: channel.ChannelName,
                                                          inputMessageContent:
                                                          new
                                                          InputTextMessageContent($"{text}\nЗаходите - " +
                                                                                  channel.InviteLink))
                    {
                        Url         = "https://" + channel.LinkChannel,
                        Title       = channel.ChannelName,
                        Description = text,
                        HideUrl     = true,
                        ThumbUrl    = channel.PhotoLink != "" ? channel.PhotoLink : "https://bipbap.ru/wp-content/uploads/2017/10/0_8eb56_842bba74_XL-640x400.jpg",
                        ThumbHeight = 48,
                        ThumbWidth  = 48,
                        ReplyMarkup = button.ToArray()
                    });
                }
            }
            try
            {
                await BotClient.AnswerInlineQueryAsync(e.InlineQuery.Id, list, isPersonal : true, cacheTime : 0);
            }
            catch (Exception b) { Log.Logging(b); }
        }