public void SendAudio(AudioEventArgs args) { Telegram.Logs.Log.Write("DialogDetailsViewModel.SendAudio file_name=" + args.OggFileName); if (string.IsNullOrEmpty(args.OggFileName)) { return; } Telegram.Logs.Log.Write("DialogDetailsViewModel.SendAudio check_disable_feature file_name=" + args.OggFileName); var id = TLLong.Random(); var accessHash = TLLong.Random(); var oggFileName = string.Format("audio{0}_{1}.mp3", id, accessHash); var wavFileName = Path.GetFileNameWithoutExtension(oggFileName) + ".wav"; long size = 0; using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) { storage.MoveFile(args.OggFileName, oggFileName); using (var file = storage.OpenFile(oggFileName, FileMode.Open, FileAccess.Read)) { size = file.Length; } var wavStream = args.PcmStream.GetWavAsMemoryStream(16000, 1, 16); using (var file = new IsolatedStorageFileStream(wavFileName, FileMode.OpenOrCreate, storage)) { wavStream.Seek(0, SeekOrigin.Begin); wavStream.CopyTo(file); file.Flush(); } } var opus = new TelegramClient_Opus.WindowsPhoneRuntimeComponent(); var bytes = opus.GetWaveform(ApplicationData.Current.LocalFolder.Path + "\\" + oggFileName); var resultSamples = bytes.Length; var bites2 = new BitArray(5 * bytes.Length); var count = 0; for (var i = 0; i < bytes.Length; i++) { var result = bytes[i]; var bit1 = result >> 0 & 0x1; var bit2 = result >> 1 & 0x1; var bit3 = result >> 2 & 0x1; var bit4 = result >> 3 & 0x1; var bit5 = result >> 4 & 0x1; bites2[count] = Convert.ToBoolean(bit1); bites2[count + 1] = Convert.ToBoolean(bit2); bites2[count + 2] = Convert.ToBoolean(bit3); bites2[count + 3] = Convert.ToBoolean(bit4); bites2[count + 4] = Convert.ToBoolean(bit5); count = count + 5; } var bytesCount = (resultSamples * 5) / 8 + (((resultSamples * 5) % 8) == 0 ? 0 : 1); var waveformBytes = new byte[bytesCount]; bites2.CopyTo(waveformBytes, 0); var waveform = waveformBytes != null?TLString.FromBigEndianData(waveformBytes) : TLString.Empty; var audioAttribute = new TLDocumentAttributeAudio46 { Flags = new TLInt((int)DocumentAttributeAudioFlags.Voice), Duration = new TLInt((int)args.Duration) }; if (waveformBytes != null) { audioAttribute.Waveform = waveform; } var attributes = new TLVector <TLDocumentAttributeBase> { audioAttribute }; var document = new TLDocument54 { Id = id, AccessHash = accessHash, Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), //Duration = new TLInt((int)args.Duration), MimeType = new TLString("audio/ogg"), Size = new TLInt((int)size), Thumb = new TLPhotoSizeEmpty { Type = TLString.Empty }, DCId = new TLInt(0), Version = new TLInt(0), Attributes = attributes }; var channel = With as TLChannel; var isChannel = channel != null && !channel.IsMegaGroup; var media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = document, Caption = TLString.Empty, IsoFileName = oggFileName, NotListened = !isChannel }; var message = GetMessage(TLString.Empty, media); message.NotListened = !isChannel; Telegram.Logs.Log.Write(string.Format("DialogDetailsViewModel.SendAudio start sending file_name={0} rnd_id={1}", args.OggFileName, message.RandomId)); BeginOnUIThread(() => { var previousMessage = InsertSendingMessage(message); message.NotifyOfPropertyChange(() => message.Media); IsEmptyDialog = Items.Count == 0 && (_messages == null || _messages.Count == 0) && LazyItems.Count == 0; BeginOnThreadPool(() => CacheService.SyncSendingMessage( message, previousMessage, m => SendAudioInternal(message, args))); }); }
public void SendAudio(AudioEventArgs args) { var chat = Chat as TLEncryptedChat; if (chat == null) { return; } if (string.IsNullOrEmpty(args.OggFileName)) { return; } var dcId = TLInt.Random(); var id = TLLong.Random(); var accessHash = TLLong.Random(); var oggFileName = String.Format("audio{0}_{1}.mp3", id, accessHash); var wavFileName = Path.GetFileNameWithoutExtension(oggFileName) + ".wav"; long size = 0; using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) { storage.MoveFile(args.OggFileName, oggFileName); using (var file = storage.OpenFile(oggFileName, FileMode.Open, FileAccess.Read)) { size = file.Length; } var wavStream = Wav.GetWavAsMemoryStream(args.PcmStream, 16000, 1, 16); using (var file = new IsolatedStorageFileStream(wavFileName, FileMode.OpenOrCreate, storage)) { wavStream.Seek(0, SeekOrigin.Begin); wavStream.CopyTo(file); file.Flush(); } } var fileLocation = new TLEncryptedFile { Id = id, AccessHash = accessHash, DCId = dcId, Size = new TLInt((int)size), KeyFingerprint = new TLInt(0), FileName = new TLString(Path.GetFileName(oggFileName)) }; var keyIV = GenerateKeyIV(); TLDecryptedMessageMediaBase decryptedMediaAudio; var encryptedChat17 = chat as TLEncryptedChat17; if (encryptedChat17 != null) { if (encryptedChat17.Layer.Value >= Constants.MinSecretChatWithAudioAsDocumentsLayer) { var opus = new TelegramClient_Opus.WindowsPhoneRuntimeComponent(); var bytes = opus.GetWaveform(ApplicationData.Current.LocalFolder.Path + "\\" + oggFileName); var resultSamples = bytes.Length; var bites2 = new BitArray(5 * bytes.Length); var count = 0; for (var i = 0; i < bytes.Length; i++) { var result = bytes[i]; var bit1 = result >> 0 & 0x1; var bit2 = result >> 1 & 0x1; var bit3 = result >> 2 & 0x1; var bit4 = result >> 3 & 0x1; var bit5 = result >> 4 & 0x1; bites2[count] = Convert.ToBoolean(bit1); bites2[count + 1] = Convert.ToBoolean(bit2); bites2[count + 2] = Convert.ToBoolean(bit3); bites2[count + 3] = Convert.ToBoolean(bit4); bites2[count + 4] = Convert.ToBoolean(bit5); count = count + 5; } var bytesCount = (resultSamples * 5) / 8 + (((resultSamples * 5) % 8) == 0 ? 0 : 1); var waveformBytes = new byte[bytesCount]; bites2.CopyTo(waveformBytes, 0); var waveform = waveformBytes != null?TLString.FromBigEndianData(waveformBytes) : TLString.Empty; var audioAttribute = new TLDocumentAttributeAudio46 { Flags = new TLInt((int)DocumentAttributeAudioFlags.Voice), Duration = new TLInt((int)args.Duration) }; if (waveformBytes != null) { audioAttribute.Waveform = waveform; } var attributes = new TLVector <TLDocumentAttributeBase> { audioAttribute }; decryptedMediaAudio = new TLDecryptedMessageMediaDocument45 { Thumb = TLString.Empty, ThumbW = new TLInt(0), ThumbH = new TLInt(0), MimeType = new TLString("audio/ogg"), Size = new TLInt((int)size), Key = keyIV.Item1, IV = keyIV.Item2, Attributes = attributes, Caption = TLString.Empty, NotListened = true, File = fileLocation, UploadingProgress = 0.001 }; } else { decryptedMediaAudio = new TLDecryptedMessageMediaAudio17 { Duration = new TLInt((int)args.Duration), MimeType = new TLString("audio/ogg"), Size = new TLInt((int)size), Key = keyIV.Item1, IV = keyIV.Item2, UserId = new TLInt(StateService.CurrentUserId), File = fileLocation, UploadingProgress = 0.001 }; } } else { decryptedMediaAudio = new TLDecryptedMessageMediaAudio { Duration = new TLInt((int)args.Duration), //MimeType = new TLString("audio/ogg"), Size = new TLInt((int)size), Key = keyIV.Item1, IV = keyIV.Item2, UserId = new TLInt(StateService.CurrentUserId), File = fileLocation, UploadingProgress = 0.001 }; } var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaAudio, chat, true); //var message45 = decryptedTuple.Item1 as TLDecryptedMessage45; //if (message45 != null && message45.IsVoice()) //{ // message45.NotListened = true; //} BeginOnUIThread(() => { InsertSendingMessage(decryptedTuple.Item1); NotifyOfPropertyChange(() => DescriptionVisibility); }); BeginOnThreadPool(() => CacheService.SyncDecryptedMessage(decryptedTuple.Item1, chat, cachedMessage => SendAudioInternal(decryptedTuple.Item2))); }
public async void SendDocument(StorageFile file) { if (file == null) { return; } var properties = await file.GetBasicPropertiesAsync(); var size = properties.Size; //file.Properties.GetImagePropertiesAsync() if (!CheckDocumentSize(size)) { MessageBox.Show(string.Format(AppResources.MaximumFileSizeExceeded, MediaSizeConverter.Convert((int)Telegram.Api.Constants.MaximumUploadedFileSize)), AppResources.Error, MessageBoxButton.OK); return; } // to get access to the file with StorageFile.GetFileFromPathAsync in future AddFileToFutureAccessList(file); var thumb = await GetFileThumbAsync(file); //create document var document = new TLDocument54 { Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), FileName = new TLString(Path.GetFileName(file.Name)), MimeType = new TLString(file.ContentType), Size = new TLInt((int)size), Thumb = thumb ?? new TLPhotoSizeEmpty { Type = TLString.Empty }, DCId = new TLInt(0), Version = new TLInt(0) }; if (string.Equals(document.FileExt, "webp", StringComparison.OrdinalIgnoreCase) && document.DocumentSize < Telegram.Api.Constants.StickerMaxSize) { document.MimeType = new TLString("image/webp"); document.Attributes.Add(new TLDocumentAttributeSticker56 { Flags = new TLInt(0), Alt = TLString.Empty, Stickerset = new TLInputStickerSetEmpty(), MaskCoords = null }); var fileName = document.GetFileName(); await file.CopyAsync(ApplicationData.Current.LocalFolder, fileName, NameCollisionOption.ReplaceExisting); } else if (string.Equals(document.FileExt, "mp3", StringComparison.OrdinalIgnoreCase)) { var musicProperties = await file.Properties.GetMusicPropertiesAsync(); if (musicProperties != null) { var documentAttributeAudio = new TLDocumentAttributeAudio46(); documentAttributeAudio.Flags = new TLInt(0); documentAttributeAudio.Duration = new TLInt((int)musicProperties.Duration.TotalSeconds); if (!string.IsNullOrEmpty(musicProperties.Title)) { documentAttributeAudio.Title = new TLString(musicProperties.Title); } if (!string.IsNullOrEmpty(musicProperties.Artist)) { documentAttributeAudio.Performer = new TLString(musicProperties.Artist); } document.Attributes.Add(documentAttributeAudio); } } var media = new TLMessageMediaDocument75 { Flags = new TLInt(0), FileId = TLLong.Random(), Document = document, Caption = TLString.Empty, IsoFileName = file.Path, File = file }; var message = GetMessage(TLString.Empty, media); BeginOnUIThread(() => { var previousMessage = InsertSendingMessage(message); IsEmptyDialog = Items.Count == 0 && (_messages == null || _messages.Count == 0) && LazyItems.Count == 0; BeginOnThreadPool(() => CacheService.SyncSendingMessage( message, previousMessage, m => SendDocumentInternal(message, file))); }); }
private void ProcessBotInlineResult(TLMessage45 message, TLBotInlineResultBase resultBase, TLInt botId) { message.InlineBotResultId = resultBase.Id; message.InlineBotResultQueryId = resultBase.QueryId; message.ViaBotId = botId; var botInlineMessageMediaVenue = resultBase.SendMessage as TLBotInlineMessageMediaVenue; var botInlineMessageMediaGeo = resultBase.SendMessage as TLBotInlineMessageMediaGeo; if (botInlineMessageMediaVenue != null) { message._media = new TLMessageMediaVenue72 { Title = botInlineMessageMediaVenue.Title, Address = botInlineMessageMediaVenue.Address, Provider = botInlineMessageMediaVenue.Provider, VenueId = botInlineMessageMediaVenue.VenueId, VenueType = TLString.Empty, Geo = botInlineMessageMediaVenue.Geo }; } else if (botInlineMessageMediaGeo != null) { message._media = new TLMessageMediaGeo { Geo = botInlineMessageMediaGeo.Geo }; } var botInlineMessageMediaContact = resultBase.SendMessage as TLBotInlineMessageMediaContact; if (botInlineMessageMediaContact != null) { message._media = new TLMessageMediaContact82 { PhoneNumber = botInlineMessageMediaContact.PhoneNumber, FirstName = botInlineMessageMediaContact.FirstName, LastName = botInlineMessageMediaContact.LastName, UserId = new TLInt(0), VCard = TLString.Empty }; } var mediaResult = resultBase as TLBotInlineMediaResult; if (mediaResult != null) { if (TLString.Equals(mediaResult.Type, new TLString("voice"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty, NotListened = !(With is TLChannel) }; message.NotListened = !(With is TLChannel); } else if (TLString.Equals(mediaResult.Type, new TLString("audio"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("sticker"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("file"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("gif"), StringComparison.OrdinalIgnoreCase) && mediaResult.Document != null) { message._media = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("photo"), StringComparison.OrdinalIgnoreCase) && mediaResult.Photo != null) { message._media = new TLMessageMediaPhoto75 { Flags = new TLInt(0), Photo = mediaResult.Photo, Caption = TLString.Empty }; } else if (TLString.Equals(mediaResult.Type, new TLString("game"), StringComparison.OrdinalIgnoreCase)) { var game = new TLGame { Flags = new TLInt(0), Id = new TLLong(0), AccessHash = new TLLong(0), ShortName = mediaResult.Id, Title = mediaResult.Title ?? TLString.Empty, Description = mediaResult.Description ?? TLString.Empty, Photo = mediaResult.Photo ?? new TLPhotoEmpty { Id = new TLLong(0) }, Document = mediaResult.Document }; message._media = new TLMessageMediaGame { Game = game, SourceMessage = message }; } } var result = resultBase as TLBotInlineResult; if (result != null) { var isVoice = TLString.Equals(result.Type, new TLString("voice"), StringComparison.OrdinalIgnoreCase); var isAudio = TLString.Equals(result.Type, new TLString("audio"), StringComparison.OrdinalIgnoreCase); var isFile = TLString.Equals(result.Type, new TLString("file"), StringComparison.OrdinalIgnoreCase); if (isFile || isAudio || isVoice) { var document = result.Document as TLDocument22; if (document == null) { string fileName = null; if (result.ContentUrl != null) { var fileUri = new Uri(result.ContentUrlString); try { fileName = Path.GetFileName(fileUri.LocalPath); } catch (Exception ex) { } if (fileName == null) { fileName = "file.ext"; } } document = new TLDocument54 { Id = new TLLong(0), AccessHash = new TLLong(0), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), FileName = new TLString(fileName), MimeType = result.ContentType ?? TLString.Empty, Size = new TLInt(0), Thumb = new TLPhotoSizeEmpty { Type = TLString.Empty }, DCId = new TLInt(0), Version = new TLInt(0) }; if (isVoice || isAudio) { var documentAttributeAudio = new TLDocumentAttributeAudio46 { Duration = result.Duration ?? new TLInt(0), Title = result.Title ?? TLString.Empty, Performer = null, Voice = isVoice }; document.Attributes.Add(documentAttributeAudio); } //message._status = MessageStatus.Failed; } var mediaDocument = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = document, Caption = TLString.Empty }; message._media = mediaDocument; mediaDocument.NotListened = isVoice && !(With is TLChannel); message.NotListened = isVoice && !(With is TLChannel); } else if (TLString.Equals(result.Type, new TLString("gif"), StringComparison.OrdinalIgnoreCase)) { var document = result.Document; if (document != null) { var mediaDocument = new TLMessageMediaDocument75 { Flags = new TLInt(0), Document = document, Caption = TLString.Empty }; message._media = mediaDocument; } } else if (TLString.Equals(result.Type, new TLString("photo"), StringComparison.OrdinalIgnoreCase)) { Telegram.Api.Helpers.Execute.ShowDebugMessage(string.Format("w={0} h={1}\nthumb_url={2}\ncontent_url={3}", result.W, result.H, result.ThumbUrl, result.ContentUrl)); var location = new TLFileLocation { DCId = new TLInt(1), VolumeId = TLLong.Random(), LocalId = TLInt.Random(), Secret = TLLong.Random() }; var cachedSize = new TLPhotoCachedSize { Type = new TLString("s"), W = result.W ?? new TLInt(90), H = result.H ?? new TLInt(90), Location = location, Bytes = TLString.Empty, TempUrl = result.ThumbUrlString ?? result.ContentUrlString //Size = new TLInt(0) }; var size = new TLPhotoSize { Type = new TLString("m"), W = result.W ?? new TLInt(90), H = result.H ?? new TLInt(90), Location = location, TempUrl = result.ContentUrlString, Size = new TLInt(0) }; if (!string.IsNullOrEmpty(result.ThumbUrlString)) { //ServicePointManager.ServerCertificateValidationCallback += new var webClient = new WebClient(); webClient.OpenReadAsync(new Uri(result.ThumbUrlString, UriKind.Absolute)); webClient.OpenReadCompleted += (sender, args) => { if (args.Cancelled) { return; } if (args.Error != null) { return; } var fileName = String.Format("{0}_{1}_{2}.jpg", location.VolumeId, location.LocalId, location.Secret); using (var stream = args.Result) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (store.FileExists(fileName)) { return; } using (var file = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { const int BUFFER_SIZE = 128 * 1024; var buf = new byte[BUFFER_SIZE]; var bytesread = 0; while ((bytesread = stream.Read(buf, 0, BUFFER_SIZE)) > 0) { var position = stream.Position; stream.Position = position - 10; var tempBuffer = new byte[10]; var resultOk = stream.Read(tempBuffer, 0, tempBuffer.Length); file.Write(buf, 0, bytesread); } } } } if (!string.IsNullOrEmpty(result.ContentUrlString)) { webClient.OpenReadAsync(new Uri(result.ContentUrlString, UriKind.Absolute)); webClient.OpenReadCompleted += (sender2, args2) => { if (args2.Cancelled) { return; } if (args2.Error != null) { return; } using (var stream = args2.Result) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (store.FileExists(fileName)) { return; } using (var file = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { const int BUFFER_SIZE = 128 * 1024; var buf = new byte[BUFFER_SIZE]; int bytesread = 0; while ((bytesread = stream.Read(buf, 0, BUFFER_SIZE)) > 0) { file.Write(buf, 0, bytesread); } } } } }; } }; } var photo = new TLPhoto56 { Flags = new TLInt(0), Id = TLLong.Random(), AccessHash = TLLong.Random(), Date = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now), Sizes = new TLVector <TLPhotoSizeBase> { cachedSize, size } }; var mediaPhoto = new TLMessageMediaPhoto75 { Flags = new TLInt(0), Photo = photo, Caption = TLString.Empty }; message._media = mediaPhoto; } } var messageText = resultBase.SendMessage as TLBotInlineMessageText; if (messageText != null) { message.Message = messageText.Message; message.Entities = messageText.Entities; if (messageText.NoWebpage) { } } var mediaAuto = resultBase.SendMessage as TLBotInlineMessageMediaAuto75; if (mediaAuto != null) { message.Message = mediaAuto.Caption; message.Entities = mediaAuto.Entities; } if (resultBase.SendMessage != null && resultBase.SendMessage.ReplyMarkup != null) { message.ReplyMarkup = resultBase.SendMessage.ReplyMarkup; } }