ETH官方钱包

前往
大廳
主題

linebotJSON輸出方法

小受NECO | 2021-06-26 10:30:38 | 巴幣 0 | 人氣 255

//這裡是自定義內容的CLASS,可以操作後放進下面的jsonMaler當參數
public class replyData{
    public string headCode,title,content,//頭碼、標題,文字說明
    imgUri,//圖片位址
    btnLb1,btnTx1,btnLb2,btnTx2,btnLb3,btnTx3,btnLb4,btnTx4;//按鈕1~4的標題與使用者答覆
    public replyData(string hc,string tt,string ct,
    string img,string bl1,string bt1,string bl2,string bt2,
    string bl3,string bt3,string bl4,string bt4){
        this.headCode=hc;
        this.title=tt;
        this.content=ct;
        this.imgUri=img;
        this.btnLb1=bl1;
        this.btnTx1=bt1;
        this.btnLb2=bl2;
        this.btnTx2=bt2;
        this.btnLb3=bl3;
        this.btnTx3=bt3;
        this.btnLb4=bl4;
        this.btnTx4=bt4;
    }
}

//這裡是LINE FLEX格式的CLASS
public class Action
    {
        public string type { get; set; }
        public string label { get; set; }
        public string data { get; set; }
        public string text { get; set; }
    }

    public class Hero
    {
        public string type { get; set; }
        public string url { get; set; }
        public string size { get; set; }
        public string aspectRatio { get; set; }
        public string aspectMode { get; set; }
        public Action action { get; set; }
    }

    public class Content
    {
        public string type { get; set; }
        public string text { get; set; }
        public string weight { get; set; }
        public string size { get; set; }
        public string style { get; set; }
        public string height { get; set; }
        public Action action { get; set; }
        public Hero hero { get; set; }
        public Body body { get; set; }
        public Footer footer { get; set; }
    }

    public class Body
    {
        public string type { get; set; }
        public string layout { get; set; }
        public List<Content> contents { get; set; }
    }

    public class Footer
    {
        public string type { get; set; }
        public string layout { get; set; }
        public string spacing { get; set; }
        public List<Content> contents { get; set; }
        public int flex { get; set; }
    }

    public class Root
    {
        public string type { get; set; }
        public string altText { get; set; }
        public Content contents { get; set; }
    }

//這裡開始是回傳JSON字串的方法
        private string jsonMaker(replyData myReplyData){
            Root flex=new Root();
            flex.type="flex";
            flex.altText="還沒點開時看到的訊息";
            flex.contents=new Content();//這邊的contents是整個flex的內容
            flex.contents.type="bubble";
            flex.contents.hero=new Hero();//hero是放圖片的地方
            flex.contents.hero.type="image";
            flex.contents.hero.url="圖片網址,請自行替換";
            flex.contents.hero.size="full";
            flex.contents.hero.aspectRatio="20:13";
            flex.contents.hero.aspectMode="fit";
            flex.contents.hero.action=new Action();//這邊的action是hero的動作,我們沒有要他做什麼,所以照抄就好
            flex.contents.hero.action.type="postback";
            flex.contents.hero.action.label="action";
            flex.contents.hero.action.data="none";
            flex.contents.body=new Body();//body是放標題與文字的地方
            flex.contents.body.type="box";
            flex.contents.body.layout="vertical";
            flex.contents.body.contents=new List<Content>();
            Content tmpContent=new Content();//這邊的content是用來把資料裝起來再塞進陣列用的
            tmpContent.type="text";
            tmpContent.text="標題內容";//這邊可以換成你自己的flex標題變數
            tmpContent.weight="bold";
            tmpContent.size="xl";
            flex.contents.body.contents.Add(tmpContent);//把剛才的tmpContent(標題)塞進陣列
            tmpContent=new Content();//只要把tmpContent重置就可以重複利用,不會殘留資料
            tmpContent.type="text";
            tmpContent.text="文字說明";//這邊可以換成你自己的flex文字說明變數
            flex.contents.body.contents.Add(tmpContent);//把剛才的tmpContent(文字說明)塞進陣列
            flex.contents.footer=new Footer();//footer是用來放按鈕選項的地方
            flex.contents.footer.type="box";
            flex.contents.footer.layout="vertical";
            flex.contents.footer.contents=new List<Content>();
            tmpContent=new Content();//一樣把tmpContent拿來回收再利用,要來做按鈕了
            tmpContent.type="button";
            tmpContent.style="link";
            tmpContent.height="sm";
            tmpContent.action=new Action();//這邊的action是按鈕的動作,我們要給他兩個東西
            tmpContent.action.type="message";
            //一個是使用者他們看到的按鈕選項
            tmpContent.action.label="按鈕1";//這邊可以換成你自己的按鈕1文字變數
            //一個是使用者按下去之後會回答的文字
            tmpContent.action.text="回答1";//這邊可以換成你自己的回答1文字變數
            flex.contents.footer.contents.Add(tmpContent);//把按鈕1塞進去
            tmpContent=new Content();//來做第二個按鈕,跟上面的步驟一樣,跟得上吧?
            tmpContent.type="button";
            tmpContent.style="link";
            tmpContent.height="sm";
            tmpContent.action=new Action();
            tmpContent.action.type="message";
            tmpContent.action.label="按鈕2";
            tmpContent.action.text="回答2";
            flex.contents.footer.contents.Add(tmpContent);//把按鈕2塞進去
            tmpContent=new Content();//來做第三個按鈕
            tmpContent.type="button";
            tmpContent.style="link";
            tmpContent.height="sm";
            tmpContent.action=new Action();
            tmpContent.action.type="message";
            tmpContent.action.label="按鈕3";
            tmpContent.action.text="回答3";
            flex.contents.footer.contents.Add(tmpContent);//把按鈕3塞進去
            tmpContent=new Content();//這邊只是要放個底線做結尾而已,不要緊張
            tmpContent.type="separator";//對,這東西就是底線
            flex.contents.footer.contents.Add(tmpContent);//這條底線塞進去就大功告成啦>w<
            flex.contents.footer.flex=0;//這行應該是什麼結尾吧,我也不知道

            string replyJson="[";//這就是我們要輸出用的字串

            var options = new JsonSerializerOptions {
                IgnoreNullValues=true,//在序列化的過程中無視null值
                IgnoreReadOnlyProperties=true//序列化的過程中無視唯讀屬性
            };//設定一下序列化選項

            replyJson+=JsonSerializer.Serialize(flex,options);//術式展開!JSON.シリアライザー!
            replyJson+="]";

            return replyJson;
        }
送禮物贊助創作者 !
0
留言

創作回應

相關創作

更多創作