forked from chomado/GoogleHomeHack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleHome.cs
58 lines (52 loc) · 2.2 KB
/
GoogleHome.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using VoiceTextWebAPI.Client;
namespace ChomadoVoice
{
public static class GoogleHome
{
[FunctionName("GoogleHome")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req
, /* Azure Blob Storage(ファイル置き場) への出力 */ [Blob("mp3/voice.mp3", FileAccess.ReadWrite)] CloudBlockBlob mp3Out
, TraceWriter log
)
{
log.Info("C# HTTP trigger function processed a request.");
var data = await req.Content.ReadAsAsync<Models.DialogFlowResponseModel>();
//log.Info(data);
var say = data.Result.ResolvedQuery;
// VoiceText Web API に投げる処理
var voiceTextClient = new VoiceTextClient
{
APIKey = Keys.APIKeys.VoiceTextWebApiKey,
Speaker = Speaker.Bear,
Emotion = Emotion.Anger,
EmotionLevel = EmotionLevel.High,
Format = Format.MP3
};
var bytes = await voiceTextClient.GetVoiceAsync(text: say);
// Azure Blob Storage への書き込み(保存)
await mp3Out.UploadFromByteArrayAsync(buffer: bytes, index: 0, count: bytes.Length);
// Azure Blob Storage に書き込まれた mp3 にアクセスするための URL
var mp3Url = mp3Out.Uri;
var result = req.CreateResponse(HttpStatusCode.OK, new
{
// Google Home に喋らせたい文言を渡す。(この場合mp3)
speech = $"<speak><audio src='{mp3Url}' /></speak>",
// Google Assistant のチャット画面上に出したい文字列
displayText = $"「{say}」"
});
result.Headers.Add("ContentType", "application/json");
return result;
}
}
}