Newer
Older
Postman / Postman.cs
@Go Akahane Go Akahane on 22 Jun 2021 1 KB first commit
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Postman
{
    public class Postman
    {
        public Configure conf { get; set; }

        public async Task<bool> Post(string text)
        {
            // 送信パラメータ作成
            var attachment = new Attachment()
            {
            };

            var mention = "";
            if (conf.Mentions.Count != 0)
            {
                mention = string.Join(" ", conf.Mentions.Select(x => $"<!{x}>").ToList());
            }


            var sendParams = new PostParams()
            {
                attachments = new List<Attachment> { attachment },
                text = $"{mention}{text}",
                channel = conf.Channel,
                username = conf.UserName,
                icon_url = conf.IconUrl
            };

            // パラメータクラスをJSONにシリアライズ。
            var paramJson = JsonConvert.SerializeObject(sendParams);

            // POSTデータを作成
            var sendData = new Dictionary<string, string>
            {
                { "payload", paramJson }
            };
            var content = new FormUrlEncodedContent(sendData);

            var hc = new HttpClient();
            var res = await hc.PostAsync(conf.PostUrl, content);

            return (res.StatusCode == System.Net.HttpStatusCode.OK);
        }


    }
}