static async void audioCommand(object sender, MessageEventArgs e) { if (e.Message.Text == "Серега") { await botClient.SendAudioAsync(e.Message.Chat, "https://zaycev.net/musicset/dl/859c3fa89d1dc037a4a88c691e931cdc/11353239.json?spa=false"); } }
public async Task <IEnumerable <long> > SendMessage(long chatId, OutMessage message) { var result = new List <long>(message.Attachments.Count + 1); GroupAttachments(message.Attachments, out var photos, out var videos, out var others); var photoMediaGroup = photos.Select(TgConverter.ToTgPhoto); var videoMediaGroup = videos.Select(TgConverter.ToTgVideo); if (photos.Count > 0) { result.AddRange((await _api.SendMediaGroupAsync(photoMediaGroup, chatId).ConfigureAwait(false)) .Select(a => (long)a.MessageId)); } if (videos.Count > 0) { result.AddRange((await _api.SendMediaGroupAsync(videoMediaGroup, chatId).ConfigureAwait(false)) .Select(a => (long)a.MessageId)); } foreach (var attachment in others) { switch (attachment.Type) { case AttachmentType.Audio: var sentAudio = await _api.SendAudioAsync(chatId, TgConverter.ToTgMedia(attachment)) .ConfigureAwait(false); result.Add(sentAudio.MessageId); break; case AttachmentType.Voice: var sentVoice = await _api.SendVoiceAsync(chatId, TgConverter.ToTgMedia(attachment)) .ConfigureAwait(false); result.Add(sentVoice.MessageId); break; default: var sendDocument = await _api.SendDocumentAsync(chatId, TgConverter.ToTgMedia(attachment)) .ConfigureAwait(false); result.Add(sendDocument.MessageId); break; } } var keyboard = message.Keyboard switch { InlineKeyboard inlineKeyboard => TgConverter.ToTgKeyboard(inlineKeyboard), ReplyKeyboard replyKeyboard => TgConverter.ToTgKeyboard(replyKeyboard), _ => message.RemoveReplyKeyboard ? new ReplyKeyboardRemove() : null }; var sentMessage = await _api.SendTextMessageAsync(chatId, message.Text, replyMarkup : keyboard) .ConfigureAwait(false); result.Add(sentMessage.MessageId); return(result); }
private static async void ForwardMessage(Message message, UserModel recipient, ITelegramBotClient botClient) { _ = message.Type switch { MessageType.Unknown => await botClient.SendTextMessageAsync(recipient.TelegramId, "Bad message!"), MessageType.Text => await botClient.SendTextMessageAsync(recipient.TelegramId, message.Text), MessageType.Photo => await botClient.SendPhotoAsync(recipient.TelegramId, new InputOnlineFile(message.Photo[0].FileId)), MessageType.Audio => await botClient.SendAudioAsync(recipient.TelegramId, new InputOnlineFile(message.Audio.FileId)), MessageType.Video => await botClient.SendVideoAsync(recipient.TelegramId, new InputOnlineFile(message.Video.FileId)), MessageType.Voice => await botClient.SendVoiceAsync(recipient.TelegramId, new InputOnlineFile(message.Voice.FileId)), MessageType.Document => await botClient.SendDocumentAsync(recipient.TelegramId, new InputOnlineFile(message.Document.FileId)), MessageType.Sticker => await botClient.SendStickerAsync(recipient.TelegramId, new InputOnlineFile(message.Sticker.FileId)), MessageType.Contact => await botClient.SendContactAsync(recipient.TelegramId, message.Contact.PhoneNumber, message.Contact.FirstName, message.Contact.LastName), MessageType.VideoNote => await botClient.SendVideoNoteAsync(recipient.TelegramId, message.VideoNote.FileId), }; }
public static async void SendSound(string path, string message = null) { bot = new TelegramBotClient(Token); try { using (var stream = File.Open(path, FileMode.Open, FileAccess.Read)) { FileToSend fts = new FileToSend(stream, Path.GetFileName(path)); await bot.SendAudioAsync(Chat_ID, new InputOnlineFile(stream, Path.GetFileName(path)), message); } } catch (Exception ex) { SendMessage($"[ERROR] {ex.Message}"); } }
private async Task SendSoundWhenExists(Message message, Story story) { await _telegramBotClient.SendChatActionAsync(message.Chat.Id, ChatAction.RecordAudio); var soundPath = $"Assets/sounds/{ story.SoundFile}"; if (IsNullOrEmpty(story.SoundFile) || System.IO.File.Exists(soundPath) == false) { await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, "Ops! This story does not have sound. 😥"); } else { await _telegramBotClient.SendTextMessageAsync(message.Chat.Id, $"On the other hand I am sharing listening. 🎊"); await using var audioStream = System.IO.File.OpenRead($"Assets/sounds/{story.SoundFile}"); await _telegramBotClient.SendAudioAsync(message.Chat.Id, new InputMedia(audioStream, story.Title)); } }
private void SendAudioPost(IPostModel post) { if (post.Attachments == null) { throw new ArgumentException("Images Post: post.attachments = null"); } if (post.Attachments.Length == 0) { throw new ArgumentException("Images Post: post.attachments.length = 0"); } var temp = bot.SendAudioAsync( ChannelID, new InputMedia(post.Attachments[0]), post.Text); temp.Wait(); Console.WriteLine(temp.Result.Text); }
private static async void Bot_OnMessege(object sender, MessageEventArgs e) { var text = e?.Message.Text; if (text == null) { return; } Console.WriteLine($"You received new messege {text} from {e.Message.Chat.FirstName} , {e.Message.Chat.Username}, {e.Message.Chat.Id} "); if (text == "/start") { await botClient.SendTextMessageAsync(e.Message.Chat.Id, e.Message.Chat.Username + " Hey Pidor").ConfigureAwait(false); return; } if (text == "/Baton") { await botClient.SendPhotoAsync(e.Message.Chat.Id, "https://sun9-57.userapi.com/c625819/v625819495/20533/nkJHRbVW_Yo.jpg", "I'am BIG BOSS").ConfigureAwait(false); return; } if (text == "/Yakuba") { await botClient.SendAudioAsync(e.Message.Chat.Id, "https://raw.githubusercontent.com/Oh-Shit-God-Damn/telegram-bot-blackWildw/master/blackWildwBot/music/Yakuba.mp3", "KING OF SPERMA"); return; } if (text == "/YakubaFapTime") { await botClient.SendAnimationAsync(e.Message.Chat.Id, "https://2gifs.ru/images/p78.media.tumblr.com/43fdb08744c97936a8e0576ed8f3a357/tumblr_omde5oZxvG1v2bt9eo1_500.gif"); return; } else { await botClient.SendTextMessageAsync(e.Message.Chat.Id, text : $"I don't know what is <{text}> :(").ConfigureAwait(false); return; } }
public async Task <Session> ExecuteCommand(MessageEventArgs e, Session session) { try { await _botClient.SendAudioAsync(e.Message.Chat, "https://github.com/TelegramBots/book/raw/master/src/docs/audio-guitar.mp3" ); using (var stream = System.IO.File.OpenRead("../media/voice.ogg")) { await _botClient.SendVoiceAsync( chatId : e.Message.Chat, voice : stream, duration : 36 ); } } catch (Exception exception) { throw new Exception("Exception occured in AudioCommand: " + exception.Message); } return(null); }
public async Task SendUtiPutiAudioMessageWithId(MessageEventArgs e) { string audioId = "CQACAgIAAxkBAAIBpF549HtiosQMES-Yz-i76lpIcm_oAAJgBgACBrvJSw3hgnD01_VhGAQ"; Message message = await _botClient.SendAudioAsync(e.Message.Chat.Id, audioId); }
async Task HandleTextMessage(Message message) { Chat chat = message.Chat; string originalMsg = message.Text.Replace("@PanderetaBot", ""); string[] args = originalMsg.Split(" "); string command = args[0]; switch (command) { case "/addapodo": if (args.Length == 1) { await botClient.SendTextMessageAsync( chatId : chat, text : $"Indicá bien el apodo papá" ); return; } if (!database.apodos.Exists(a => a.Equals(args[1]))) { database.apodos.Add(args[1]); await botClient.SendTextMessageAsync( chatId : chat, text : $"Agregaste el apodo **{args[1]}**" ); database.SaveDB(); } break; case "/addfrase": if (args.Length == 1) { await botClient.SendTextMessageAsync( chatId : chat, text : $"Indicá bien la frase papá" ); return; } string frase = originalMsg.Replace(command, ""); if (!database.frases.Exists(a => a.Equals(frase))) { database.frases.Add(frase); await botClient.SendTextMessageAsync( chatId : chat, text : $"Agregaste la frase **{frase}**" ); database.SaveDB(); } break; case "/apodo": await botClient.SendTextMessageAsync( chatId : chat, text : database.apodos.RandomElement() ); break; case "/lopa": await botClient.SendTextMessageAsync( chatId : chat, text : database.frases.RandomElement() ); break; case "/audio": if (database.audios.Count() == 0) { await botClient.SendTextMessageAsync( chatId : chat, text : $"No hay audios cargados :(. Te dejo una banana {char.ConvertFromUtf32(0x1F34C)}" ); return; } await botClient.SendAudioAsync( chatId : chat, audio : database.audios.RandomElement() ); break; case "/foto": if (database.photos.Count() == 0) { await botClient.SendTextMessageAsync( chatId : chat, text : $"No hay fotos cargadas :(. Te dejo un camello {char.ConvertFromUtf32(0x1F42A)}" ); return; } await botClient.SendPhotoAsync( chatId : chat, photo : database.photos.RandomElement() ); break; } }
// async OnMessage event task for the bot // it has to be async so the bot can be always listening private async static void BotClient_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e) { // Normal text message. Checks wether the message does have text. // in case it does, the bot answers. #region Text Message if (e.Message.Text != null) { Console.WriteLine($"Received text message in chat {e.Message.Chat.Id}."); // the bot sends asynchronously a message to the chat await botClient.SendTextMessageAsync( chatId : e.Message.Chat.Id, text : "You said:\n" + e.Message.Text ); } #endregion // Normal text message, with all the properties // a text message can have. #region Text message with properties Message textMessage = await botClient.SendTextMessageAsync( chatId : e.Message.Chat, // or a chat id: 123456789 text : "Trying *all the parameters* of `sendMessage` method", parseMode : ParseMode.Markdown, disableNotification : true, replyToMessageId : e.Message.MessageId, replyMarkup : new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl( "Check sendMessage method", "https://core.telegram.org/bots/api#sendmessage" )) ); #endregion // Almost all of the methods for sending messages return you // the message you just sent. #region Messages return you the message you sent (as an object) Console.WriteLine( $"{textMessage.From.FirstName} sent message {textMessage.MessageId} " + $"to chat {textMessage.Chat.Id} at {textMessage.Date.ToLocalTime()}. " + // if you don't use ToLocalTime(), the bot will show the UTC time. $"It is a reply to message {textMessage.ReplyToMessage.MessageId} " + $"and has {textMessage.Entities.Length} message entities." ); #endregion // Photo message. It gets the image from a URL on the internet. // All images can have captions. You can use HTML or Markdown on them. #region Photo Message Message photoMessage = await botClient.SendPhotoAsync( chatId : e.Message.Chat, photo : "https://github.com/TelegramBots/book/raw/master/src/docs/photo-ara.jpg", caption : "<b>Ara bird</b>. <i>Source</i>: <a href=\"https://pixabay.com\">Pixabay</a>", parseMode : ParseMode.Html ); #endregion // Sticker message. Sticker files should be in WebP format. // Sends two messages. The first one gets the sticker by passing HTTP URL // to WebP sticker file, and the second by reusing the file_id of the // first sticker (kinda like a cache?) #region Sticker Messages Message stickerMessage1 = await botClient.SendStickerAsync( chatId : e.Message.Chat, sticker : "https://github.com/TelegramBots/book/raw/master/src/docs/sticker-fred.webp" ); Message stickerMessage2 = await botClient.SendStickerAsync( chatId : e.Message.Chat, sticker : stickerMessage1.Sticker.FileId ); #endregion // Audio message. Opens it in Media Player. MP3 Format. // Telegram can read metadata from the MP3 file (that's why // there are commented properties; the bot gets the same // properties, but from the file metadata!) #region Audio Message Message audioMessage = await botClient.SendAudioAsync( e.Message.Chat, "https://github.com/TelegramBots/book/raw/master/src/docs/audio-guitar.mp3" /*, * performer: "Joel Thomas Hunger", * title: "Fun Guitar and Ukulele", * duration: 91 // in seconds */ ); #endregion // Voice message. Not opened in Media Player (played directly // on the chat). OGG Format. // In this case, the OGG file is on our disk! #region Voice Message Message voiceMessage; using (var stream = System.IO.File.OpenRead("C:/Users/Biel/source/repos/TelegramBot/voice-nfl_commentary.ogg")) { voiceMessage = await botClient.SendVoiceAsync( chatId : e.Message.Chat, voice : stream, duration : 36 ); } #endregion // You can send MP4 files as a regular video or as a video note. // Other video formats must be sent as a file. // Video message. They can have caption, reply, and reply markup // (like other multimedia messages) // You can optionally specify the duration and the resolution // of the video. In this case the video is streamed, // meaning the user can partly watch the video on stream, without // having to download it completely. #region Video Message Message videoMessage = await botClient.SendVideoAsync( chatId : e.Message.Chat, video : "https://raw.githubusercontent.com/TelegramBots/book/master/src/docs/video-countdown.mp4", thumb : "https://raw.githubusercontent.com/TelegramBots/book/master/src/2/docs/thumb-clock.jpg", supportsStreaming : true ); #endregion // Video note message. They are shown in circles to the user. // They are usually short (1 minute or less). // You can send a video note only by the video file or // reusing the file_id of another video note. // (sending it by its HTTP URL is not supported currently) #region Video Note Message Message videoNoteMessage; using (var stream = System.IO.File.OpenRead("C:/Users/step/Source/Repos/TelegramBotExamples/video-waves.mp4")) { videoNoteMessage = await botClient.SendVideoNoteAsync( chatId : e.Message.Chat, videoNote : stream, duration : 47, length : 360 // value of width/height ); } #endregion // Poll message. They can be sent only to groups and channels. // You can optionally send a keyboard with a poll, // both inline and a regular one. #region Poll Message Message pollMessage = await botClient.SendPollAsync( chatId : "@group_or_channel_username", question : "Did you ever hear the tragedy of Darth Plagueis The Wise?", options : new [] { "Yes for the hundredth time!", "No, who's that?" } ); #endregion // To stop the poll, you need to know original chat and message ids // (you get them from the Poll object you get on SendPollAsync, the pollMessage) #region Closing a Poll Poll poll = await botClient.StopPollAsync( chatId : pollMessage.Chat.Id, messageId : pollMessage.MessageId ); #endregion // Filtering messages for commands if (e.Message.Text == "/sayhello") { Message sayHello = await botClient.SendTextMessageAsync ( chatId : e.Message.Chat.Id, text : "Hello! Glad to see you!" ); } if (e.Message.Text == "/saygoodbye") { Message sayHello = await botClient.SendTextMessageAsync ( chatId : e.Message.Chat.Id, text : "Bye! Have a nice day!" ); } }
private static async void OnMessage(object sender, MessageEventArgs messageEventArgs) { Message message = messageEventArgs.Message; Chat chatInfo = message.Chat; string messageText = message.Text.ToLower(); if (messageText != null) { ITelegramBotClient client = TelegramBot.Instance.Client; Console.WriteLine($"{chatInfo.FirstName}: envío {message.Text}"); switch (messageText) { case "/commands": case "/comandos": StringBuilder commandsStringBuilder = new StringBuilder("Lista de Comandos:\n") .Append("/commands\n") .Append("/comandos\n") .Append("/hola\n") .Append("/hello\n") .Append("/hi\n") .Append("/foto\n") .Append("/photo\n") .Append("/sticker\n") .Append("/link\n") .Append("/voice\n") .Append("/audio\n"); await client.SendTextMessageAsync( chatId : chatInfo.Id, text : commandsStringBuilder.ToString()); break; case "/hola": case "/hello": case "/hi": await client.SendTextMessageAsync( chatId : chatInfo.Id, text : $"Hola, ¿cómo estás {chatInfo.FirstName}? 👋😀"); break; case "/foto": case "/photo": using (Stream stream = System.IO.File.OpenRead("../../Assets/kotlin.jpg")) { await client.SendPhotoAsync( chatId : chatInfo.Id, photo : stream, caption : "<b>Kotlin</b>. <i>Source</i>: <a href=\"https://www.meme-arsenal.com/en/create/meme/497325\">Meme Arsenal</a>", parseMode : ParseMode.Html); } break; case "/sticker": using (Stream stream = System.IO.File.OpenRead("../../Assets/csharp.webp")) { await client.SendStickerAsync( chatId : chatInfo.Id, sticker : stream); } break; case "/link": using (Stream stream = System.IO.File.OpenRead("../../Assets/ACDC_Back_In_Black.ogg")) { await client.SendTextMessageAsync( chatId : chatInfo.Id, text : "https://www.youtube.com/watch?v=A6ZqNQdJPjc"); } break; case "/voice": using (Stream stream = System.IO.File.OpenRead("../../Assets/ACDC_Back_In_Black.ogg")) { await client.SendVoiceAsync( chatId : chatInfo.Id, voice : stream, duration : 25); } break; case "/audio": using (Stream stream = System.IO.File.OpenRead("../../Assets/darthVader.mp3")) { await client.SendAudioAsync( chatId : chatInfo.Id, title : "I'm your father", performer : "Darth Vader", thumb : "https://acib.es/wp-content/uploads/2015/11/arton427.jpg", audio : stream, duration : 3); } break; default: await client.SendTextMessageAsync( chatId : chatInfo.Id, text : $"{chatInfo.FirstName}, no comprendo lo que dices 😕" ); break; } } }