Created
May 10, 2017 22:23
-
-
Save SebastianGaud/7b2889012bd5552942d509b93ea6f652 to your computer and use it in GitHub Desktop.
Retrieve Chat Id from getUpdates and send a Message to the first recipient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Main(string[] args) | |
{ | |
List<Result> result = MakeRequest().Result; | |
SendMessage(result); | |
Console.Read(); | |
} | |
public static async void SendMessage(List<Result> result) | |
{ | |
TelegramBotClient client = new TelegramBotClient( ApiKey ); | |
Result res = result[0]; | |
Console.WriteLine( res.Message.From.Id ); | |
var t = await client.SendTextMessageAsync( res.Message.From.Id , "Ciao pelandrone!" ); | |
Console.WriteLine( t.From.FirstName ); | |
} | |
public static async Task<List<Result>> MakeRequest() | |
{ | |
HttpClient client = new HttpClient(); | |
client.BaseAddress = new Uri( ApiEndPoint + ApiKey + "/" ); | |
// Add an Accept header for JSON format. | |
client.DefaultRequestHeaders.Accept.Add( | |
new MediaTypeWithQualityHeaderValue( "application/json" ) ); | |
// List data response. | |
HttpResponseMessage response = client.GetAsync( client.BaseAddress + Command.GetUpdates ).Result; // Blocking call! | |
if (!response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine("{0} ({1})", response.StatusCode, response.ReasonPhrase); | |
throw new Exception(); | |
} | |
else | |
{ | |
string data = await response.Content.ReadAsStringAsync(); | |
Console.WriteLine(data); | |
Response result = JsonConvert.DeserializeObject<Response>(data); | |
return result.Result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment