Open palashjhabak opened 3 years ago
When we get JSON object at our end. we user controller that processes an incoming POST request and we use a model to get every data from a JSON object. Based on values we got from the JSON object in API controller class we can respond to the POST request. suppose in .net use :
[ApiController] public class ControllerClassName : ControllerBase { [HttpPost] public IActionResult Post(JObject payload) { return Ok("Got the request"); } }
- here ControllerClassName is the Endpoint we provided in setwebhook URL
- But if we want to send text back in chat then we need TelegramBotClient using only this we can send text in chat.
Cool. So what are the different types of incoming JSON format? We will have to create models for all the different types on incoming JSON requests because we might get requests from a private chat, a channel or a group and in different forms, a text, an attachment or anything else. So first and foremost lets model all the types of incoming data and take it form there.
yes, we have different incoming JSON formats for different updates in chat. according to telegram bot API documentation, there are 50+ different models but we can implement only the models we need in this project. when we get a JSON object there is a property named "type" which can be “private”, “group”, “supergroup” or “channel”, using this values we can identify where the message was sent
yes, we have different incoming JSON formats for different updates in chat. according to telegram bot API documentation, there are 50+ different models but we can implement only the models we need in this project. when we get a JSON object there is a property named "type" which can be “private”, “group”, “supergroup” or “channel”, using this values we can identify where the message was sent
ok so in that case, document the models which we need. for now we just need text inputs from a private chat or a telegram group, we dont need channel for now, neither do we need messages from user other than texts.
yes, we have different incoming JSON formats for different updates in chat. according to telegram bot API documentation, there are 50+ different models but we can implement only the models we need in this project. when we get a JSON object there is a property named "type" which can be “private”, “group”, “supergroup” or “channel”, using this values we can identify where the message was sent
ok so in that case, document the models which we need. for now we just need text inputs from a private chat or a telegram group, we dont need channel for now, neither do we need messages from user other than texts.
have created a model for Private messages and Group Messages. suppose our model class name is NewMessage then the model class content will be this
public partial class NewMessage
{
[JsonProperty("update_id")]
public long UpdateId { get; set; }
[JsonProperty("message")]
public Message Message { get; set; }
}
public partial class Message
{
[JsonProperty("message_id")]
public long MessageId { get; set; }
[JsonProperty("from")]
public From From { get; set; }
[JsonProperty("chat")]
public Chat Chat { get; set; }
[JsonProperty("date")]
public long Date { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}
public partial class Chat
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
public partial class From
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("is_bot")]
public bool IsBot { get; set; }
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("language_code")]
public string LanguageCode { get; set; }
}
have created a model for Private messages and Group Messages. suppose our model class name is NewMessage then the model class content will be this
public partial class NewMessage { [JsonProperty("update_id")] public long UpdateId { get; set; } [JsonProperty("message")] public Message Message { get; set; } } public partial class Message { [JsonProperty("message_id")] public long MessageId { get; set; } [JsonProperty("from")] public From From { get; set; } [JsonProperty("chat")] public Chat Chat { get; set; } [JsonProperty("date")] public long Date { get; set; } [JsonProperty("text")] public string Text { get; set; } } public partial class Chat { [JsonProperty("id")] public long Id { get; set; } [JsonProperty("first_name")] public string FirstName { get; set; } [JsonProperty("last_name")] public string LastName { get; set; } [JsonProperty("username")] public string Username { get; set; } [JsonProperty("type")] public string Type { get; set; } } public partial class From { [JsonProperty("id")] public long Id { get; set; } [JsonProperty("is_bot")] public bool IsBot { get; set; } [JsonProperty("first_name")] public string FirstName { get; set; } [JsonProperty("last_name")] public string LastName { get; set; } [JsonProperty("username")] public string Username { get; set; } [JsonProperty("language_code")] public string LanguageCode { get; set; } }
ok. So will we have just one model and a type property in it or different models?
have created a model for Private messages and Group Messages. suppose our model class name is NewMessage then the model class content will be this
public partial class NewMessage { [JsonProperty("update_id")] public long UpdateId { get; set; } [JsonProperty("message")] public Message Message { get; set; } } public partial class Message { [JsonProperty("message_id")] public long MessageId { get; set; } [JsonProperty("from")] public From From { get; set; } [JsonProperty("chat")] public Chat Chat { get; set; } [JsonProperty("date")] public long Date { get; set; } [JsonProperty("text")] public string Text { get; set; } } public partial class Chat { [JsonProperty("id")] public long Id { get; set; } [JsonProperty("first_name")] public string FirstName { get; set; } [JsonProperty("last_name")] public string LastName { get; set; } [JsonProperty("username")] public string Username { get; set; } [JsonProperty("type")] public string Type { get; set; } } public partial class From { [JsonProperty("id")] public long Id { get; set; } [JsonProperty("is_bot")] public bool IsBot { get; set; } [JsonProperty("first_name")] public string FirstName { get; set; } [JsonProperty("last_name")] public string LastName { get; set; } [JsonProperty("username")] public string Username { get; set; } [JsonProperty("language_code")] public string LanguageCode { get; set; } }
ok. So will we have just one model and a type property in it or different models?
if we want to text in only groups and private message then this one model is enough, but if we want to communicate in a channel then we have to expand the Chat class of this model and create another model for channel messages.
When we get JSON object at our end. we user controller that processes an incoming POST request and we use a model to get every data from a JSON object. Based on values we got from the JSON object in API controller class we can respond to the POST request. suppose in .net use :
here ControllerClassName is the Endpoint we provided in setwebhook URL
But if we want to send text back in chat then we need TelegramBotClient using only this we can send text in chat.