如何在Telegram中为自定义消息设置ChannelData?

时间:2016-10-12 20:50:51

标签: c# botframework telegram-bot

我想在Telegram中发送自定义消息。如何使用C#设置ChannelData属性?应该设置一个名称值的字符串,如此链接中提到的CustomMessage?你能给我一个示例代码吗?

2 个答案:

答案 0 :(得分:1)

如果您将数据作为Json对象(JObject)提供,您可以发送一个字符串,但它更干净;不是一个字符串。这是一些代码示例(来自我的blog post);展示如何发送Facebook的快速回复。你需要为Telegram做类似的事情;制作它所需的特定对象,替换属性的名称等。

var channelData = JObject.FromObject(new
    {
        quick_replies = new dynamic[]
        {
            new
            {
                content_type = "text",
                title = "Blue",
                payload = "DEFINED_PAYLOAD_FOR_PICKING_BLUE",
                image_url = "https://cdn3.iconfinder.com/data/icons/developperss/PNG/Blue%20Ball.png"
            },
            new
            {
                content_type = "text",
                title = "Green",
                payload = "DEFINED_PAYLOAD_FOR_PICKING_GREEN",
                image_url = "https://cdn3.iconfinder.com/data/icons/developperss/PNG/Green%20Ball.png"
            },
            new
            {
                content_type = "text",
                title = "Red",
                payload = "DEFINED_PAYLOAD_FOR_PICKING_RED",
            }
        }
    });

    reply.ChannelData = channelData;

答案 1 :(得分:1)

是。它可以是字符串化的json。例如,要发送贴纸,您可以使用以下代码:

var channelData = "{";
channelData +=       "'method': 'sendSticker',";
channelData +=       "'parameters':";
channelData +=       "{";
channelData +=          "'sticker':";
channelData +=          "{";
channelData +=             "'url': 'http://www.thisiscolossal.com/wp-content/uploads/2014/03/120430.gif',"
channelData +=             "'mediaType': 'image/gif'";
channelData +=          "}";
channelData +=        "}";
channelData +=    "}";
var reply = new Activity();
reply.ChannelData = channelData;