@@ -171,29 +171,45 @@ private async Task<UploadPhotoResult> UploadPhoto(string uploadUrl, string name,
171171 /// <returns></returns>
172172 private async Task < UploadDocResult > UploadAudioMessage ( string uploadUrl , string name , byte [ ] binaryContent , CancellationToken token )
173173 {
174- using var httpClient = _httpClientFactory . CreateClient ( ) ;
175174 // convert to ogg in order to meet VK requirements
176175 var oggContent = _audioConvertor . Convert ( binaryContent , new AudioInfo ( )
177176 {
178177 AudioFormat = AudioFormat . Ogg ,
179178 Bitrate = 16000
180179 } ) ;
181180
181+ return await PushDocument < UploadDocResult > ( uploadUrl , name , oggContent , token ) ;
182+ }
183+
184+ private async Task < TResult > PushDocument < TResult > ( string uploadUrl , string name , byte [ ] binContent ,
185+ CancellationToken token )
186+ {
187+ using var httpClient = _httpClientFactory . CreateClient ( ) ;
182188 var content = new MultipartFormDataContent
183189 {
184190 {
185- new ByteArrayContent ( oggContent , 0 , oggContent . Length ) ,
191+ new ByteArrayContent ( binContent , 0 , binContent . Length ) ,
186192 "file" ,
187- $ "{ name } { Guid . NewGuid ( ) } .ogg "
193+ $ "{ Path . GetFileNameWithoutExtension ( name ) } { Guid . NewGuid ( ) } .{ Path . GetExtension ( name ) } "
188194 }
189195 } ;
190196
191197 var response = await httpClient . PostAsync ( uploadUrl , content , token ) ;
192198 var resultString = await response . Content . ReadAsStringAsync ( ) ;
193199
194- return JsonSerializer . Deserialize < UploadDocResult > ( resultString ) ;
200+ return JsonSerializer . Deserialize < TResult > ( resultString ) ;
195201 }
196202
203+ /// <summary>
204+ /// Uploads a document message (binaries)
205+ /// </summary>
206+ /// <param name="uploadUrl"></param>
207+ /// <param name="binaryContent"></param>
208+ /// <param name="token"></param>
209+ /// <returns></returns>
210+ private async Task < UploadDocResult > UploadDocMessage ( string uploadUrl , string name , byte [ ] binaryContent , CancellationToken token )
211+ => await PushDocument < UploadDocResult > ( uploadUrl , name , binaryContent , token ) ;
212+
197213 /// <summary>
198214 /// Uploads a video (binaries)
199215 /// </summary>
@@ -316,6 +332,47 @@ public async Task<VkSendAudioResponse> SendAudioMessageAsync(VkSendMessageReques
316332 }
317333
318334
335+ public async Task < VkSendDocumentResponse > SendDocsMessageAsync ( VkSendMessageRequest vkMessageRequest , string name , byte [ ] binaryContent , CancellationToken token )
336+ {
337+ try
338+ {
339+ var address = await GetDocsUploadAddress ( vkMessageRequest , "doc" , token ) ;
340+
341+ if ( address ? . Response == default ) throw new BotException ( "Sending doc error: no upload server address!" ) ;
342+
343+ var uploadedDoc = await UploadDocMessage ( address . Response . UploadUrl , name , binaryContent , token ) ;
344+
345+ if ( uploadedDoc ? . File == default ) throw new BotException ( "Sending doc error: no file uploaded!" ) ;
346+
347+ using var httpClient = _httpClientFactory . CreateClient ( ) ;
348+ var request = new HttpRequestMessage ( HttpMethod . Post ,
349+ ApiUtils . GetMethodUri ( "https://api.vk.com" ,
350+ "docs.save" ,
351+ new
352+ {
353+ file = uploadedDoc . File ,
354+ access_token = _apiKey ,
355+ v = ApiVersion
356+ } ) ) ;
357+ request . Content = ApiUtils . GetMethodMultipartFormContent ( new
358+ {
359+ doc = uploadedDoc . File
360+ } ,
361+ snakeCase : true ) ;
362+
363+ var response = await httpClient . SendAsync ( request , token ) ;
364+ var resultString = await response . Content . ReadAsStringAsync ( ) ;
365+
366+ return JsonSerializer . Deserialize < VkSendDocumentResponse > ( resultString ) ;
367+ }
368+ catch ( Exception ex )
369+ {
370+ _logger . LogError ( ex , "Error uploading media" ) ;
371+ }
372+
373+ return default ;
374+ }
375+
319376 /// <summary>
320377 /// The main public method for sending a video
321378 /// seems to be prohibited for bots in communities
0 commit comments