9

I'm developing a Telegram bot in C# but have difficulty with implementing Message type. According to API documentation, chat field can be either of type User or of type GroupChat. How do I implement that in C#?

So far I could only come up with following code using Newtonsoft.Json:

public class Update {
....
  [JsonProperty("chat")]
  public User chat { get; set; }

  [JsonProperty("chat")]
  public GroupChat group_chat { get; set; }
....
}

But it doesn't work with my WebAPI 2 controller method since I deserialize Message using FromBody attribute:

public async Task<HttpResponseMessage> Post(string token, [FromBody] Update update)

(type Update has a field message of type Message)

Is there any better way to implement Message type?

2
  • A better way to say it would be that a class property could be one of two different types.
    – crashmstr
    Aug 5, 2015 at 19:19
  • @crashmstr right, I renamed the question
    – Azimuth
    Aug 6, 2015 at 10:54

3 Answers 3

3

Since you are supposed to handle seemingly two completely different objects in one field I don't think you can use strongly-typed objects but you can use a dynamic type for the chat field such as

public class Telegram
{
    public int message_id { get; set; }
    public dynamic chat { get; set; }
}

Then you can access the variables and check if they are null or not to understand if it's a user or a group chat:

static void Main(string[] args)
{
    string json = @"{ ""chat"": { ""id"": 1, ""first_name"": ""my_first_name"", ""last_name"": ""my_last_name"",  ""username"": ""my_username"" }, ""message_id"": 123 }";
    var telegram = Newtonsoft.Json.JsonConvert.DeserializeObject<Telegram>(json);
    string name = telegram.chat.first_name; // name = my_first_name
    string title = telegram.chat.title; // title = null
}
1

You could build your Message type as an interface (IMessage), and then implement it with GroupMessage and UserMessage classes.

interface IMessage
{
   int id;

   //...

   object chat {get;}
}

public class GroupMessage : IMessage
{
   public int id;
   public GroupChat group;
   public object chat {get {return group;} }
}

public class UserMessage : IMessage
{
   public int id;
   public User user;
   public object chat {get {return user;} }
}

But what I would really do is handle this at a higher level. Looking at the docs here, rather than a complex object that can handle all of this I would think in terms of sending or receiving a message. Then I would think of all of the actions you might want to do when receiving a message. From there I would have an object that can raise an event for each of those actions. When you receive data (don't think of it as a message), you parse the data and perhaps raise several events from what was one Telegram message object. Some of those events may require class definitions for the data needed by the event, but now we're talking something that's much narrower in scope; a better fit for the Single Responsibility Principle.

Going the other direction (sending data rather than receiving), the object would have methods for the different kinds of things you might do when sending data. Some of those methods may require classes for the data needed. When possible, use the same classes you did for the receiving events. It might even be just one Send() method with many overloads.

0

I have faced this issue in Java as well. As both C# and Java are strong typed languages, my issue was similar. I solved this by creating a generic Chat class. This Chat class contains the properties from User as well as from GroupChat. The class also has the methods isUser(), isGroupChat(), asUser() and asGroupChat().

I have to admit I have no knowledge of C#, so I can not write an example in that language for you, but here is the body of the Chat class, in Java:

@SerializedName("id")
private int id;

@SerializedName("first_name")
private String firstName;

@SerializedName("last_name")
private String lastName;

@SerializedName("username")
private String username;

@SerializedName("title")
private String title;

public boolean isUser() {
    return title == null;
}

public boolean isGroupChat() {
    return !isUser();
}

public User asUser() {
    return new User(id, firstName, username, lastName);
}

public GroupChat asGroupChat() {
    return new GroupChat(id, title);
}

It's quite verbose and adds duplicate code, so if anyone has any additions, I'd like to hear them as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.