Skip to content

Commit c52edf6

Browse files
authored
Merge pull request #113 from sj-distributor/optimization-record
Add restaurant
2 parents a6752cf + f75f10c commit c52edf6

File tree

7 files changed

+18
-109
lines changed

7 files changed

+18
-109
lines changed

src/SmartTalk.Api/Controllers/PhoneOrderController.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Serilog;
66
using SmartTalk.Messages.Commands.PhoneOrder;
77
using SmartTalk.Messages.Dto.SpeechMatics;
8+
using SmartTalk.Messages.Enums.PhoneOrder;
89
using SmartTalk.Messages.Requests.PhoneOrder;
910

1011
namespace SmartTalk.Api.Controllers;
@@ -59,15 +60,15 @@ public async Task<IActionResult> AddPhoneOrderConversationsAsync([FromBody] AddP
5960

6061
[HttpPost("record/receive")]
6162
[ProducesResponseType(StatusCodes.Status200OK)]
62-
public async Task<IActionResult> ReceivePhoneOrderRecordAsync([FromForm] IFormFile file)
63+
public async Task<IActionResult> ReceivePhoneOrderRecordAsync([FromForm] IFormFile file, [FromForm] string restaurant)
6364
{
6465
var ms = new MemoryStream();
6566

6667
await file.CopyToAsync(ms).ConfigureAwait(false);
6768

6869
var fileContent = ms.ToArray();
6970

70-
await _mediator.SendAsync(new ReceivePhoneOrderRecordCommand { RecordName = file.FileName, RecordContent = fileContent }).ConfigureAwait(false);
71+
await _mediator.SendAsync(new ReceivePhoneOrderRecordCommand { RecordName = file.FileName, RecordContent = fileContent, Restaurant = restaurant}).ConfigureAwait(false);
7172

7273
return Ok();
7374
}

src/SmartTalk.Core/Services/PhoneOrder/PhoneOrderService.Record.cs

+7-24
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ public async Task ReceivePhoneOrderRecordAsync(ReceivePhoneOrderRecordCommand co
5757
{
5858
if (command.RecordName.IsNullOrEmpty()) return;
5959

60-
var recordInfo = ExtractPhoneOrderRecordInfoFromRecordName(command.RecordName);
60+
var recordInfo = ExtractPhoneOrderRecordInfoFromRecordName(command.RecordName, command.Restaurant);
6161

6262
Log.Information("Phone order record information: {@recordInfo}", recordInfo);
63-
63+
6464
if (await CheckOrderExistAsync(recordInfo.OrderDate.AddHours(-8), cancellationToken).ConfigureAwait(false)) return;
6565

6666
var transcription = await _speechToTextService.SpeechToTextAsync(
@@ -430,37 +430,20 @@ private async Task<string> UploadRecordFileAsync(string fileName, byte[] fileCon
430430
return uploadResponse.Attachment.FileUrl;
431431
}
432432

433-
private PhoneOrderRecordInformationDto ExtractPhoneOrderRecordInfoFromRecordName(string recordName)
433+
private PhoneOrderRecordInformationDto ExtractPhoneOrderRecordInfoFromRecordName(string recordName, string restaurant)
434434
{
435435
var time = string.Empty;
436-
var phoneNumber = string.Empty;
437-
438-
var regexInOut = new Regex(@"(?:in-(\d+)|out-\d+-(\d+))-.*-(\d+)\.\d+\.wav");
436+
437+
var regexInOut = new Regex(@"-(\d+)\.");
439438
var match = regexInOut.Match(recordName);
440439

441440
if (match.Success)
442-
{
443-
time = match.Groups[3].Value;
444-
phoneNumber = match.Groups[1].Success ? match.Groups[1].Value : match.Groups[2].Value;
445-
}
441+
time = match.Groups[1].Value;
446442

447443
return new PhoneOrderRecordInformationDto
448444
{
449445
OrderDate = DateTimeOffset.FromUnixTimeSeconds(long.Parse(time)),
450-
Restaurant = phoneNumber[0] switch
451-
{
452-
'3' or '6' => PhoneOrderRestaurant.JiangNanChun,
453-
'5' or '7' => PhoneOrderRestaurant.XiangTanRenJia,
454-
'8' or '9' or '2' or '0' => PhoneOrderRestaurant.MoonHouse,
455-
_ => throw new Exception("Phone Number not exist")
456-
},
457-
WorkWeChatRobotKey = phoneNumber[0] switch
458-
{
459-
'3' or '6' => _phoneOrderSetting.GetSetting("江南春"),
460-
'5' or '7' => _phoneOrderSetting.GetSetting("湘潭人家"),
461-
'8' or '9' or '2' or '0' => _phoneOrderSetting.GetSetting("福满楼"),
462-
_ => throw new Exception("Phone Number not exist")
463-
}
446+
Restaurant = (PhoneOrderRestaurant)Enum.Parse(typeof(PhoneOrderRestaurant), restaurant, true)
464447
};
465448
}
466449

src/SmartTalk.Core/Services/SpeechMatics/SpeechMaticsService.cs

-67
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ public async Task HandleTranscriptionCallbackAsync(HandleTranscriptionCallbackCo
6565
var audioContent = await _smartTalkHttpClientFactory.GetAsync<byte[]>(record.Url, cancellationToken).ConfigureAwait(false);
6666

6767
await _phoneOrderService.ExtractPhoneOrderRecordAiMenuAsync(speakInfos, record, audioContent, cancellationToken).ConfigureAwait(false);
68-
69-
await SendWorkWeChatRobotNotifyAsync(audioContent, new PhoneOrderRecordInformationDto
70-
{
71-
OrderDate = record.CreatedDate,
72-
Restaurant = record.Restaurant,
73-
WorkWeChatRobotKey = record.Restaurant switch
74-
{
75-
PhoneOrderRestaurant.JiangNanChun => _phoneOrderSetting.GetSetting("江南春"),
76-
PhoneOrderRestaurant.XiangTanRenJia => _phoneOrderSetting.GetSetting("湘潭人家"),
77-
PhoneOrderRestaurant.MoonHouse => _phoneOrderSetting.GetSetting("福满楼"),
78-
_ => throw new Exception("Restaurant not exist")
79-
}
80-
}, record.TranscriptionText, cancellationToken).ConfigureAwait(false);
8168
}
8269
catch (Exception e)
8370
{
@@ -89,60 +76,6 @@ public async Task HandleTranscriptionCallbackAsync(HandleTranscriptionCallbackCo
8976
}
9077
}
9178

92-
public async Task SendWorkWeChatRobotNotifyAsync(byte[] recordContent, PhoneOrderRecordInformationDto recordInfo, string transcription, CancellationToken cancellationToken)
93-
{
94-
await _weChatClient.SendWorkWechatRobotMessagesAsync(recordInfo.WorkWeChatRobotUrl,
95-
new SendWorkWechatGroupRobotMessageDto
96-
{
97-
MsgType = "text",
98-
Text = new SendWorkWechatGroupRobotTextDto
99-
{
100-
Content = $"----------{recordInfo.Restaurant.GetDescription()}-PST {recordInfo.OrderDate.ToString("yyyy/MM/dd HH:mm:ss")}----------"
101-
}
102-
}, cancellationToken);
103-
104-
var splitAudios = await ConvertAndSplitAudioAsync(recordContent, secondsPerAudio: 60, cancellationToken: cancellationToken).ConfigureAwait(false);
105-
106-
await SendMultiAudioMessagesAsync(splitAudios, recordInfo, cancellationToken).ConfigureAwait(false);
107-
108-
await _weChatClient.SendWorkWechatRobotMessagesAsync(
109-
recordInfo.WorkWeChatRobotUrl, new SendWorkWechatGroupRobotMessageDto
110-
{
111-
MsgType = "text", Text = new SendWorkWechatGroupRobotTextDto { Content = transcription }
112-
}, CancellationToken.None);
113-
114-
await _weChatClient.SendWorkWechatRobotMessagesAsync(
115-
recordInfo.WorkWeChatRobotUrl, new SendWorkWechatGroupRobotMessageDto
116-
{
117-
MsgType = "text", Text = new SendWorkWechatGroupRobotTextDto { Content = "-------------------------End-------------------------" }
118-
}, CancellationToken.None);
119-
}
120-
121-
public async Task<List<byte[]>> ConvertAndSplitAudioAsync(byte[] record, int secondsPerAudio, CancellationToken cancellationToken)
122-
{
123-
var amrAudio = await _ffmpegService.ConvertWavToAmrAsync(record, "", cancellationToken: cancellationToken).ConfigureAwait(false);
124-
125-
return await _ffmpegService.SplitAudioAsync(amrAudio, secondsPerAudio, "amr", cancellationToken: cancellationToken).ConfigureAwait(false);
126-
}
127-
128-
public async Task SendMultiAudioMessagesAsync(List<byte[]> audios, PhoneOrderRecordInformationDto recordInfo, CancellationToken cancellationToken)
129-
{
130-
foreach (var audio in audios)
131-
{
132-
var uploadResponse = await _weChatClient.UploadWorkWechatTemporaryFileAsync(
133-
recordInfo.WorkWeChatRobotUploadVoiceUrl, Guid.NewGuid() + ".amr", UploadWorkWechatTemporaryFileType.Voice, audio, cancellationToken: cancellationToken).ConfigureAwait(false);
134-
135-
if (string.IsNullOrEmpty(uploadResponse?.MediaId)) continue;
136-
137-
await _weChatClient.SendWorkWechatRobotMessagesAsync(recordInfo.WorkWeChatRobotUrl,
138-
new SendWorkWechatGroupRobotMessageDto
139-
{
140-
MsgType = "voice",
141-
Voice = new SendWorkWechatGroupRobotFileDto { MediaId = uploadResponse.MediaId }
142-
}, cancellationToken);
143-
}
144-
}
145-
14679
private List<SpeechMaticsSpeakInfoDto> StructureDiarizationResults(List<SpeechMaticsResultDto> results)
14780
{
14881
string currentSpeaker = null;

src/SmartTalk.IntegrationTests/Services/Security/SecurityFixture.cs

+1-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SmartTalk.IntegrationTests.Utils.Security;
1010
using SmartTalk.Messages.Commands.Security;
1111
using SmartTalk.Messages.Enums.Account;
12+
using SmartTalk.Messages.Enums.PhoneOrder;
1213
using SmartTalk.Messages.Requests.Security;
1314
using Xunit;
1415
using Xunit.Abstractions;
@@ -161,13 +162,4 @@ await RunWithUnitOfWork<IMediator>(async mediator =>
161162
currentUserRoles.Data.Roles[1].Name.ShouldBe("User");
162163
});
163164
}
164-
165-
[Fact]
166-
public async Task Test()
167-
{
168-
string? userName = null;
169-
170-
if (string.IsNullOrEmpty(userName) || !Regex.IsMatch(userName, @"^[a-zA-Z]+$")) _testOutputHelper.WriteLine("2");
171-
172-
}
173165
}

src/SmartTalk.Messages/Commands/PhoneOrder/ReceivePhoneOrderRecordCommand.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Mediator.Net.Contracts;
2+
using SmartTalk.Messages.Enums.PhoneOrder;
23

34
namespace SmartTalk.Messages.Commands.PhoneOrder;
45

@@ -7,4 +8,6 @@ public class ReceivePhoneOrderRecordCommand : ICommand
78
public string RecordName { get; set; }
89

910
public byte[] RecordContent { get; set; }
11+
12+
public string Restaurant { get; set; }
1013
}

src/SmartTalk.Messages/Dto/PhoneOrder/PhoneOrderRecordInformationDto.cs

-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,4 @@ public class PhoneOrderRecordInformationDto
77
public PhoneOrderRestaurant Restaurant { get; set; }
88

99
public DateTimeOffset OrderDate { get; set; }
10-
11-
public string WorkWeChatRobotKey { get; set; }
12-
13-
public string WorkWeChatRobotUrl => "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + WorkWeChatRobotKey;
14-
15-
public string WorkWeChatRobotUploadVoiceUrl => "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=" + WorkWeChatRobotKey + "&type=voice";
1610
}

src/SmartTalk.Messages/Enums/PhoneOrder/PhoneOrderRestaurant.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ public enum PhoneOrderRestaurant
1111
JiangNanChun,
1212

1313
[Description("湘谭人家")]
14-
XiangTanRenJia
14+
XiangTanRenJia,
15+
16+
[Description("悟空")]
17+
Wukong
1518
}

0 commit comments

Comments
 (0)