コード例 #1
0
        public async void ProcessSticker(object state)
        {
            Update update = state as Update;

            var commands  = Bot.Commands;
            var message   = update.Message;
            var botClient = await Bot.GetBotClientAsync();


            // for stickers
            if (message.Type == MessageType.Sticker)
            {
                var file = await botClient.GetFileAsync(message.Sticker.FileId);

                var mess = string.Format("Associated emoji: {0}\nAnimeted: {1}\nPack: {2}\nConverting ...",
                                         message.Sticker.Emoji, message.Sticker.IsAnimated, message.Sticker.SetName);

                await botClient.SendTextMessageAsync(message.Chat, mess, replyToMessageId : message.MessageId);

                var fileExtension = message.Sticker.IsAnimated ? ".tgs" : ".webp";

                try
                {
                    var localSavePath   = $"/app/tgs-to-gif/cache/{message.Sticker.FileUniqueId + fileExtension}";
                    var localUploadPath = $"/app/tgs-to-gif/cache/{message.Sticker.FileUniqueId}.gif";

                    // Download sticker from telegram
                    if (!System.IO.File.Exists(localSavePath))
                    {
                        using (var saveImageStream = new FileStream(localSavePath, FileMode.Create))
                        {
                            await botClient.DownloadFileAsync(file.FilePath, saveImageStream);
                        }
                    }

                    // If the file exists, go directly to the upload
                    if (!System.IO.File.Exists(localUploadPath))
                    {
                        //Convert tgs sticker
                        if (message.Sticker.IsAnimated == true)
                        {
                            ExecuteCommand($"/app/tgs-to-gif/bin/tgs_to_gif {localSavePath} -o {localUploadPath}");
                        }
                        //Convert webp sticker
                        else
                        {
                            ConvertImageApi apiInstance = new ConvertImageApi();
                            var             fromFormat  = "WEBP";
                            var             toFormat    = "GIF";
                            var             inputFile   = new FileStream(localSavePath, FileMode.Open); // Input file to perform the operation

                            // Image format conversion
                            byte[]       result = apiInstance.ConvertImageImageFormatConvert(fromFormat, toFormat, inputFile);
                            MemoryStream ms     = new MemoryStream(result);
                            Bitmap       bm     = new Bitmap(ms);
                            bm.Save(localUploadPath);
                        }
                    }

                    // Upload sticker to telegram
                    using (FileStream fs = System.IO.File.OpenRead(localUploadPath))
                    {
                        InputOnlineFile inputOnlineFile = new InputOnlineFile(fs, message.Sticker.FileUniqueId + ".gif");
                        await botClient.SendDocumentAsync(message.Chat, inputOnlineFile);
                    }
                }
                catch (Exception e)
                {
                    await botClient.SendTextMessageAsync(message.Chat, $"Conversion {fileExtension} error! \n " + e.Message);
                }
            }
        }
 public void Init()
 {
     instance = new ConvertImageApi();
 }