public static async Task <bool> SongSending(TelegramBotClient client, long id, string songName, string songUrl)
        {
            var website = new HtmlWeb();

            website.OverrideEncoding = System.Text.Encoding.GetEncoding("utf-8");
            try
            {
                HtmlDocument doc = website.Load(songUrl);

                var songTextNode = doc?.DocumentNode?.SelectSingleNode("//*[@class=\"b-podbor__text\"]");

                var songText = songTextNode?.InnerText;

                var songsChordsImages = new List <string>();
                foreach (var node in doc?.DocumentNode?.SelectNodes("//*[@id=\"song_chords\"]"))
                {
                    foreach (var img in node?.ChildNodes)
                    {
                        songsChordsImages.Add($"https:{img?.Attributes[0]?.Value}");
                    }
                }
                var uniqueCode = DateTime.Now.Ticks;
                if (await DownloadImages(uniqueCode, songsChordsImages))
                {
                    var content = new List <NodeElement>();
                    content.Add(new NodeElement("img", new Dictionary <string, string>
                    {
                        ["src"] = $"https://telegra.ph/file/6e0006d12cf4319268af4.jpg"
                    }));

                    var compositeImages = await CreateCompositeImages(uniqueCode, songsChordsImages.Count);

                    foreach (var image in compositeImages)
                    {
                        var url = await UploadImage(image);

                        content.Add(new NodeElement("img", new Dictionary <string, string> {
                            ["src"] = "https://telegra.ph" + url
                        }));
                    }

                    content.Add(new NodeElement("div", null, children: songText.Replace("\r\n\r\n", "\r\n").Replace("&#180;", "´")));

                    var telegraphClient = new TelegraphClient();
                    var _tokenClient    = telegraphClient.GetTokenClient($"{AppSettings.TelegraphToken }");
                    var page            = _tokenClient.CreatePageAsync(
                        title: songName,
                        authorName: "Music Accords Bot",
                        authorUrl: "https://t.me/muschords_bot",
                        returnContent: true,
                        content: content.ToArray()
                        ).Result;

                    var message = await client.SendTextMessageAsync(id, page.Url, replyMarkup : Keyboards.GetSongMenuKeyboard(true));

                    AddSongToBase(id, message.MessageId, page.Url, songName);
                    DataBase.AddSongToLibrary(songName, page.Url);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(false);
            }
            return(true);
        }
Beispiel #2
0
 static TelegraphService()
 {
     client      = new TelegraphClient();
     tokenClient = client.GetTokenClient(AppSettings.TelegraphApiKey);
 }