LoginSignup
6
6

More than 5 years have passed since last update.

Unityからchatworkにメッセージを送る

Last updated at Posted at 2016-08-14

Unityからchatworkにメッセージを送る。(Unity 5.4.0)
適当なオブジェクトにつけてインスペクタからroomidに部屋のID、tokenにAPIトークンを入れる。

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class PostMessage : MonoBehaviour
{
    public string roomid;
    public string token;

    IEnumerator Post(string message)
    {
        string url = string.Format("https://api.chatwork.com/v1/rooms/{0}/messages", roomid);
        WWWForm form = new WWWForm();
        form.AddField("body", message);
        using (UnityWebRequest req = UnityWebRequest.Post(url, form))
        {
            req.SetRequestHeader("X-ChatWorkToken", token);

            yield return req.Send();

            if (req.isError)
            {
                Debug.Log(req.error);
            }
            else
            {
                Debug.Log(req.responseCode);
                Debug.Log(req.downloadHandler.text);
                _message = "";
            }
        }
    }

    string _message = "";

    void OnGUI()
    {
        _message = GUILayout.TextField(_message, GUILayout.Width(200));

        if (GUILayout.Button("Send"))
        {
            StartCoroutine(Post(_message));
        }
    }
}

参考

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6