在Microsoft Bot Framework中获取有关用户的频道数据 [英] Get channel data about user at Microsoft Bot Framework

查看:85
本文介绍了在Microsoft Bot Framework中获取有关用户的频道数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#的Microsoft Bot Framework上制作了一个机器人.我正在尝试获取发送到我的机器人的电报数据.当我直接使用Telegram机器人API时,我可以看到有关用户的数据,例如:

I make a bot at Microsoft Bot Framework on c#. I am trying to get Telegram data that sent to my bot. When I worked with Telegram bot API directly, I can see data about user like:

{
  "update_id": 1111111,
  "message": {
    "message_id": 111111,
    "from": {
      "id": 1111111,
      "is_bot": "False",
      "first_name": "John",
      "last_name": "Dillon",
      "language_code": "en-EN"
    },
    "chat": {
      "id": 111111111,
      "first_name": "John",
      "last_name": "Dillon",
      "type": "private"
    },
    "date": 1111111,
    "text": "Hello"
  }
}

我从此json中获取first_name,然后我的机器人将消息发送为:

I grab first_name from this json and my bot sends the message like:

- Whould you like to use John as the name?

我需要在Microsoft Bot Framework上最好使用所有可能的渠道进行同样的操作.我在文档中找到了有关状态数据的信息,但是与我的问题无关.他们建议我询问用户名.

I need to do the same at Microsoft Bot Framework preferably with all channels where this is possiable. I found information about state data in documentation, but there is nothing about my issue. They advise me to ask user name.

推荐答案

尝试使用channelData.from.first_name.请注意,这仅在用户在电报中的个人资料中实际设置了他们的姓名时才起作用.这是一个可行的示例.

Try using channelData.from.first_name. Note that this will only work when the user has actually set their name in their profile in Telegram. Here is a workign example.

    var message = await result as Activity;
    string data = message.ChannelData.ToString();
    TelegramData myClass = JsonConvert.DeserializeObject<TelegramData>(data);
    var fname = myClass.message.from.first_name;

使用edit> paste special>粘贴JSON作为类创建以下类:

Created these classes with edit>paste special>paste JSON as classes:

public class TelegramData
{
    public int update_id { get; set; }
    public Message message { get; set; }
}

public class Message
{
    public int message_id { get; set; }
    public From from { get; set; }
    public Chat chat { get; set; }
    public int date { get; set; }
    public string text { get; set; }
}

public class From
{
    public int id { get; set; }
    public bool is_bot { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string username { get; set; }
    public string language_code { get; set; }
}

public class Chat
{
    public int id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string username { get; set; }
    public string type { get; set; }
}

这篇关于在Microsoft Bot Framework中获取有关用户的频道数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆