private static async void AddSticker(int userId, string pngFilename, string emoji, int messageId) { try { InputOnlineFile newSticker = new InputOnlineFile(new FileStream(Path.Combine(Path.GetTempPath(), pngFilename), FileMode.Open)); // Create new sticker set if (UserMapping[userId] == null) { UserMapping[userId] = "sohranenki_" + DateTime.Now.Ticks.ToString().Substring(8, 7) + "_by_" + BotName; Db.NewUser(userId, UserMapping[userId]); try { await Bot.CreateNewStickerSetAsync(userId, UserMapping[userId], "Сохраненки", newSticker, emoji); } catch (Exception ex) { Console.WriteLine(ex.Message + "\n" + ex.StackTrace); Db.NewMsg(Db.MsgTypes.ERROR, ex.Message, userId, ex.StackTrace); return; } await Bot.SendTextMessageAsync( chatId : userId, text : $"\u2705 Стикер успешно сохранен в [вот сюда](t.me/addstickers/{UserMapping[userId]})\\. Не забудь сохранить этот стикерпак\\.", parseMode : ParseMode.MarkdownV2 ); } // Add to existing sticker set else { try { await Bot.AddStickerToSetAsync(userId, UserMapping[userId], newSticker, emoji); } catch (Exception ex) { Console.WriteLine(ex.Message + "\n" + ex.StackTrace); Db.NewMsg(Db.MsgTypes.ERROR, ex.Message, userId, ex.StackTrace); await Bot.SendTextMessageAsync(userId, "\u274c Task failed successfully. Попробуй еще раз.", ParseMode.Default, false, false, messageId); return; } await Bot.SendTextMessageAsync(userId, "\u2705 Стикер сохранен", ParseMode.Default, false, false, messageId); } } catch (Exception ex) { Console.WriteLine(ex.Message + "\n" + ex.StackTrace); Db.NewMsg(Db.MsgTypes.ERROR, ex.Message, userId, ex.StackTrace); await Bot.SendTextMessageAsync(userId, "\u274c Task failed successfully. Попробуй еще раз.", ParseMode.Default, false, false, messageId); } }
public async Task EchoAsync(Update update) { var inputMsg = update.Message; var message = string.Empty; if (update.Type == UpdateType.Message) { var lineId = Regex.Match(inputMsg.Text, @"\d+").Value; var downloadUrl = string.Format(_config.LineStickerConfig.NormalStickerUrl, lineId); List <Telegram.Bot.Types.File> files = new List <Telegram.Bot.Types.File> (); using (var hc = new HttpClient()) { Stream imgsZip = null; try { imgsZip = await hc.GetStreamAsync(downloadUrl); } catch (Exception ex) { _logger.Log(LogLevel.Error, $"{ex}"); return; } using (ZipArchive zipFile = new ZipArchive(imgsZip)) { foreach (var entry in zipFile.Entries) { if (Regex.Match(entry.Name, @"^\[email protected]").Success) { using (Image image = Image.Load(entry.Open())) using (MemoryStream ms = new MemoryStream()) { image.Mutate(m => m.Resize(512, 512)); image.SaveAsPng(ms); ms.Seek(0, SeekOrigin.Begin); try { var file = await _botService.UploadStickerFileAsync( userId : (int)inputMsg.Chat.Id, pngSticker : new Telegram.Bot.Types.InputFiles.InputFileStream(ms) ); files.Add(file); } catch (Exception ex) { _logger.Log(LogLevel.Error, $"{ex}"); return; } } } } ; } } string tempName = $"bot{Guid.NewGuid ().ToString ("N")}_by_{_config.BotConfig.BotName}"; int firstEmojiUnicode = 0x1F601; var emojiString = char.ConvertFromUtf32(firstEmojiUnicode); var stickerTitle = "TransferBy@MartinBot"; await _botService.CreateNewStickerSetAsync( userId : (int)inputMsg.Chat.Id, name : tempName, title : stickerTitle, pngSticker : files[0].FileId, emojis : emojiString ); for (int i = 0; i < files.Count; i++) { emojiString = char.ConvertFromUtf32(firstEmojiUnicode + i); await _botService.AddStickerToSetAsync( userId : (int)inputMsg.Chat.Id, name : tempName, pngSticker : files[i].FileId, emojis : emojiString ); } var stickerSet = await _botService.GetStickerSetAsync( name : tempName ); var sticker = stickerSet.Stickers[0]; await _botService.SendStickerAsync( chatId : new ChatId(inputMsg.Chat.Id), sticker : new Telegram.Bot.Types.InputFiles.InputOnlineFile(sticker.FileId) ); } else { await _botService.SendTextMessageAsync( chatId : inputMsg.Chat.Id, text : $" Please, Send me a line sticker link " ); } }