iT邦幫忙

0

[筆記系列] C# Messager JSON

    public class JobjectChatifyFormat
    {
        public JObject GetMessage(string message)
        {
            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    })
                }
            });
            return resultJson;
        }

        public JObject GetMessageTestMuti(string message)
        {
            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    }),
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    }),
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    }),
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    }),
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    })
                    ,
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    })

                }
            });
            return resultJson;
        }

        public JObject GetMutiPicture(string img)
        {
            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {

                    JObject.FromObject(new
                    {
                        attachment = JObject.FromObject(new
                        {
                            type = "image",
                            payload = JObject.FromObject(new
                            {
                                url = img,
                                is_reusable = true
                            })
                        })
                    }),
                    JObject.FromObject(new
                    {
                        attachment = JObject.FromObject(new
                        {
                            type = "image",
                            payload = JObject.FromObject(new
                            {
                                url = img,
                                is_reusable = true
                            })
                        })
                    })
                }
            });
            return resultJson;
        }

        public JObject GetErrorMessage(string message)
        {
            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        text = $">>Error<<{message}"
                    })
                }
            });
            return resultJson;
        }

        public JObject GetExcetionMessage(string message)
        {
            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        text = $">>Excetion<<{message}"
                    })
                }
            });
            return resultJson;
        }

        public JObject GetMessageWithPicture(string img, string message)
        {
            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    }),
                    JObject.FromObject(new
                    {
                        attachment = JObject.FromObject(new
                        {
                            type = "image",
                            payload = JObject.FromObject(new
                            {
                                url = img,
                                is_reusable = true
                            })
                        })
                    })
                }
            });
            return resultJson;
        }

        public JObject GetBtnUrlWithMessage(List<string> urls, List<string> urlBtnNames, string message, string btnTitleMessage)
        {

            JArray jArray = new JArray();
            for (int i = 0; i < urls.Count; i++)
            {
                JObject j = JObject.FromObject(new
                {
                    type = "web_url",
                    url = urls[i].ToString(),
                    title = urlBtnNames[i].ToString()
                });
                jArray.Add(j);
            }

            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        text = $"{message}"
                    }),
                    JObject.FromObject(new
                    {
                        attachment = JObject.FromObject(new
                        {
                            type = "template",
                            payload = JObject.FromObject(new
                            {
                                template_type = "button",
                                text = btnTitleMessage,
                                buttons = jArray
                            })
                        })
                    })
                }
            });
            return resultJson;
        }

        public JObject GetBtnUrlWithPicture(List<string> urls, List<string> urlBtnNames, string picturePaths, string btnTitleMessage)
        {
            JArray jArray = new JArray();
            for (int i = 0; i < urls.Count; i++)
            {
                JObject j = JObject.FromObject(new
                {
                    type = "web_url",
                    url = urls[i].ToString(),
                    title = urlBtnNames[i].ToString()
                });
                jArray.Add(j);
            }

            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        attachment = JObject.FromObject(new
                        {
                            type = "image",
                            payload = JObject.FromObject(new
                            {
                                url = picturePaths,
                                is_reusable = true
                            })
                        })
                    }),
                    JObject.FromObject(new
                    {
                        attachment = JObject.FromObject(new
                        {
                            type = "template",
                            payload = JObject.FromObject(new
                            {
                                template_type = "button",
                                text = btnTitleMessage,
                                buttons = jArray
                            })
                        })
                    })
                }
            });
            return resultJson;

        }

        public JObject GetQuickRepliesText(string message, List<string> replyTexts)
        {
            JArray replies = new JArray();
            for (int i = 0; i < replyTexts.Count; i++)
            {
                JObject j = JObject.FromObject(new {
                    content_type = "text",
                    title = replyTexts[i],
                    payload = ""
                });
                replies.Add(j);
            }

            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                    {
                        JObject.FromObject(new
                        {
                            text = $"{message}",
                            quick_replies = replies
                        })
                    }
            });

            //長相:
            //resultJson = JObject.FromObject(new
            //{
            //    messages = new JArray
            //        {
            //            JObject.FromObject(new
            //            {
            //                text = $"{message}",
            //                quick_replies = new JArray
            //                {
            //                    JObject.FromObject(new
            //                    {
            //                        content_type = "text",
            //                        title = replyMessage1,
            //                        payload = "",
            //                        image_url = image
            //                    })
            //                }
            //            })
            //        }
            //});


            return resultJson;
        }

        public JObject GetQuickRepliesTextWithPicture(string message, List<string> replyTexts,string image)
        {
            JArray replies = new JArray();
            for (int i = 0; i < replyTexts.Count; i++)
            {
                JObject j = JObject.FromObject(new
                {
                    content_type = "text",
                    title = replyTexts[i],
                    payload = ""
                });
                replies.Add(j);
            }

            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                    {
                        JObject.FromObject(new
                        {
                            attachment = JObject.FromObject(new
                            {
                                type = "image",
                                payload = JObject.FromObject(new
                                {
                                    url = image,
                                    is_reusable = true
                                })
                            })
                        }),
                        JObject.FromObject(new
                        {
                            text = $"{message}",
                            quick_replies = replies
                        })
                    }
            });

            //長相:
            //resultJson = JObject.FromObject(new
            //{
            //    messages = new JArray
            //        {
            //            JObject.FromObject(new
            //            {
            //                text = $"{message}",
            //                quick_replies = new JArray
            //                {
            //                    JObject.FromObject(new
            //                    {
            //                        content_type = "text",
            //                        title = replyMessage1,
            //                        payload = "",
            //                        image_url = image
            //                    })
            //                }
            //            })
            //        }
            //});


            return resultJson;
        }

        public JObject GetLocation()
        {
            JObject resultJson = JObject.FromObject(new
            {
                messages = new JArray
                {
                    JObject.FromObject(new
                    {
                        content_type = "location"
                    })
                }
            });
            return resultJson;
        }

        public JObject GetLandscapeList(List<string> elementTitle, List<string> elementSubtitle, List<string> images, List<string> buttonUrls, List<string> buttonTitles)
    {
        JArray jarryElements = new JArray();

        for (int i = 0; i < elementTitle.Count(); i++)
        {
            JArray jobjectButtons = new JArray();
            JObject jobjectButton = JObject.FromObject(new
            {
                type = "web_url",
                url = $"{buttonUrls[i]}",
                title = $"{buttonTitles[i]}"
            });
            jobjectButtons.Add(jobjectButton);

            JObject jObjectElement = JObject.FromObject(new
            {
                title = $"{elementTitle[i]}",
                subtitle = $"{elementSubtitle[i]}",
                image_url = $"{RootHelper.GetRequestUriPath()}/Image/{images[i]}",
                buttons = jobjectButtons
            });
            jarryElements.Add(jObjectElement);
        }


        //最多10個
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);
        //jarryElements.Add(jObjectElement1);

        JObject resultJson = JObject.FromObject(new
        {
            messages = new JArray
            {
                JObject.FromObject(new
                {
                    attachment = JObject.FromObject(new
                    {
                        type = "template",
                        payload = JObject.FromObject(new
                        {
                            template_type = "generic",
                            elements = jarryElements
                        })
                    })
                })
            }
        });

        return resultJson;
    }
    }

尚未有邦友留言

立即登入留言