示例#1
0
    IEnumerator chatAPICall(string message)
    {
        // 雑談API URL
        string apiUrl = "https://api.apigw.smt.docomo.ne.jp/naturalChatting/v1/dialogue?APIKEY=" + _docomoApiToken;
        // request bodyのオブジェクトセット
        ChatRequestData requestBody = new ChatRequestData(message, _docomoAppId);
        string          requestJson = JsonUtility.ToJson(requestBody);

        byte[] postData = System.Text.Encoding.UTF8.GetBytes(requestJson);

        UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");

        request.uploadHandler   = (UploadHandler) new UploadHandlerRaw(postData);
        request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json;charset=UTF-8");
        yield return(request.SendWebRequest());

        // 通信エラーチェック
        if (request.isNetworkError)
        {
            Debug.Log(request.error);
        }
        else
        {
            if (request.responseCode == 200)
            {
                // Debug.Log(request.downloadHandler.text);
                ChatResponseJson responseJson = JsonUtility.FromJson <ChatResponseJson>(request.downloadHandler.text);
                Debug.Log(responseJson.SystemText.Expression);
                StartCoroutine(transformSpeech(responseJson.SystemText.Expression));
                // StartCoroutine(setSpeech( responseJson.SystemText.Expression ));
            }
        }
    }
示例#2
0
        public async Task <IHttpActionResult> CreateChatMessage(int chatId, [FromBody] ChatRequestData data)
        {
            DataHelper dataHelper = new DataHelper();
            object     userId;

            Request.Properties.TryGetValue("user_id", out userId);
            int uid = Convert.ToInt32(userId);

            if (string.IsNullOrEmpty(data.Message))
            {
                return(BadRequest());
            }

            try
            {
                ChatMessage chatMessage = await dataHelper.CreateChatMessage(uid, chatId, data.Message);

                UserChat userChat = await dataHelper.CreateUserChat(uid, chatId);

                User user = await dataHelper.FindUserById(uid);

                return(Ok(new { data = DataHelper.BuildChatMessageResponseData(chatMessage, user), meta = new { } }));
            }
            catch (Exception e)
            {
                // log
                return(InternalServerError());
            }
        }
示例#3
0
        public async Task <IHttpActionResult> UpdateChat(int id, [FromBody] ChatRequestData data)
        {
            DataHelper dataHelper = new DataHelper();
            object     userId;

            Request.Properties.TryGetValue("user_id", out userId);
            int uid = Convert.ToInt32(userId);

            if (string.IsNullOrEmpty(data.Name))
            {
                return(BadRequest());
            }

            try
            {
                Chat chat = await dataHelper.UpdateChat(id, uid, data.Name);

                var respData = new
                {
                    id                = chat.ChatId,
                    name              = chat.Name,
                    users             = dataHelper.FindUsersByChat(chat.ChatId),
                    last_chat_message = dataHelper.FindLastMessageByChat(chat.ChatId)
                };

                return(Ok(new { data = respData, meta = new { } }));
            }
            catch (UnauthorizedAccessException)
            {
                // log it
                return(Unauthorized());
            }
            catch (Exception)
            {
                return(InternalServerError());
            }
        }