简体   繁体   中英

How do I send a post request to telegram API from C#?

I have my telegram application with app's api_id and app's api_hash.

I used TLSharp library for implementing my own things. But now I need to use this https://core.telegram.org/method/auth.checkPhone telegram api method, but it's not implemented in TLSharp library!

I don't mind doing it all manually, but I don't know how!

I know how you send post requests in C#, example:

var response = await client.PostAsync(" http://www.example.com/index ", content);

but in this specific case I don't. Because I don't know:

1) what link should I use for sending post requests? I couldn't find it on the telegram's website.

2) what content should I pass there? Should it be just "(auth.checkPhone "+380666454343")" or maybe the whole "(auth.checkPhone "+380666454343")=(auth.checkedPhonephone_registered:(boolFalse)phone_invited:(boolFalse))" ?

So, How do I sent this post request to the telegram api? (NOT telegram bot api!)

Try to use System.Net.Http like in this example (auth request to the server):

var user = new { login = "your login", password = "your pass" };
string json = JsonConvert.SerializeObject(user);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri("server route link"); // can be like https://a100.technovik.ru:1000/api/auth/authenticate
request.Method = HttpMethod.Post;
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request);
responseText.Text =  await response.Content.ReadAsStringAsync();

I think based on a brief look, that it would be more along the lines of your second example, eg:

var phonenumber = "0123456789";
var content = 
    $@"(auth.checkPhone ""{phonenumber}"")"+
    "=(auth.checkedPhone phone_registered: (boolFalse) phone_invited:(boolFalse))";
var result = DoHttpPost("http://some.example.com/api/etc", content);

(note: I've not listed the actual mechanics of an HTTP Request here, as that is covered in plenty of detail elsewhere - not least in the other current answer supplied to you; DoHttpPost() is not a real method and exists here only as a placeholder for this process)

And since the payload of this appears to indicate the exact function and parameters required, that you'd just send it to the base api endpoint you use for everything, but I can't say for sure...

I do note they do appear to have links to source code for various apps on the site though, so perhaps you'd be better off looking there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM