Get channel data about user at Microsoft Bot Frame

2019-05-31 14:25发布

问题:

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"
  }
}

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

- Whould you like to use John as the name?

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.

回答1:

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;

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; }
}