using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; using RestSharp; using RestSharp.Authenticators; using Sony.ChartBot.Entities; using Sony.ChartBot.Entities.MessengerApi; namespace Sony.ChartBot.MessengerApi { public class MessengerApiClient { internal static string AccessToken { get { //TODO: Config? return "EAAY1wK9ZAgisBAI0MHD9M2YL7xhrKZAepEr7WvJQQR0KPfbXMCFtq3hVMo0uKZBNj5oaTIVVqqqu4qWX08WkTDgUNeT1AF5ciSoenXqeA1g8MhZBnty5ZACClXPZAyVKZA5ZAuXVeb0iGG1JbtmjYC7VZCZBpZBAGUYIqvpRwENX8tybgZDZD"; } } public class MessengerApiAuthenticator : IAuthenticator { public void Authenticate(IRestClient client, IRestRequest request) { request.AddQueryParameter("access_token", AccessToken); } } public IRestClient RestClient { get { var client = new RestClient("https://graph.facebook.com/v2.6/") { Authenticator = new MessengerApiAuthenticator() }; return client; } } public async Task GetUserDataAsync(string id) { var request = new RestRequest(id, Method.GET); var response = await RestClient.ExecuteTaskAsync(request); ValidateResponse(response); return response.Data; } public async Task SendMessageAsync(string recipientId, MessageToSend message) { var sendMessageRequest = new SendMessageRequest { recipient = new Recipient { id = recipientId }, message = message }; var request = new RestRequest("me/messages", Method.POST); request.AddJsonBody(sendMessageRequest); var response = await RestClient.ExecuteTaskAsync(request); ValidateResponse(response); } public void ClearAllCallToActions() { var requestDeleteCtaExistingThread = new RestRequest("me/thread_settings", Method.DELETE); requestDeleteCtaExistingThread.AddJsonBody(new Setting { setting_type = "call_to_actions", thread_state = "existing_thread" }); ValidateResponse(RestClient.Execute(requestDeleteCtaExistingThread)); var requestDeleteCtaNewThread = new RestRequest("me/thread_settings", Method.DELETE); requestDeleteCtaNewThread.AddJsonBody(new Setting { setting_type = "call_to_actions", thread_state = "new_thread" }); ValidateResponse(RestClient.Execute(requestDeleteCtaNewThread)); } public void AddCallToActions(IEnumerable settings) { foreach (var setting in settings) { var request = new RestRequest("me/thread_settings", Method.POST); request.AddJsonBody(setting); RestClient.Execute(request); } } private void ValidateResponse(IRestResponse response) { ValidateResponse(response, HttpStatusCode.OK); } // ReSharper disable once UnusedParameter.Local private void ValidateResponse(IRestResponse response, params HttpStatusCode[] allowedStatusCodes) { if (response == null || response.ErrorException != null || !allowedStatusCodes.Contains(response.StatusCode)) throw new Exception( $"Failed response: {response?.Request?.Resource} {response?.Request?.Method} {response?.StatusCode} {response?.Content} {response?.ErrorMessage}", response?.ErrorException); } private void ValidateResponse(IRestResponse response) { ValidateResponse(response, HttpStatusCode.OK); } private void ValidateResponse(IRestResponse response, params HttpStatusCode[] allowedStatusCodes) { ValidateResponse((IRestResponse) response, allowedStatusCodes); if (response.Data == null) throw new Exception($"Failed response: Missing data {response.Content}", response.ErrorException); } } }