using System.Collections.Generic; using System.Linq; using System.Text; using System; using System.Threading.Tasks; using RestSharp; using KitchenCommon.Interfaces; using Newtonsoft.Json; using System.Net.Http; using Microsoft.Extensions.Logging; // הוסף את ה-namespace של ILogger using System.Net.Http.Headers; public class YemotService : IYemotService { private readonly HttpClient _httpClient; // HttpClient לשליחת בקשות private readonly ILogger _logger; // אובייקט לוגינג //private const string ApiUrl = "https://private.call2all.co.il/ym/api/?token=${username}:${password}"; // ה-URL של ה-API // בנאי שמקבל את ה-httpClient ואת logger public YemotService(HttpClient httpClient, ILogger logger) { _httpClient = httpClient; // שמירה של HttpClient _logger = logger; // שמירה של ILogger } private string _token; public async Task AuthenticateAsync() { var requestData = new { username = "023132534", password = "32534" }; var json = JsonConvert.SerializeObject(requestData); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync("https://private.call2all.co.il/ym/api/authenticate", content); response.EnsureSuccessStatusCode(); var responseContent = await response.Content.ReadAsStringAsync(); var tokenData = JsonConvert.DeserializeObject(responseContent); _token = tokenData.Token; // הוסף לוג כדי לוודא שהטוקן הושג _logger.LogInformation($"Authenticated successfully, Token: {_token}"); } public async Task MakeVoiceCallAsync(string phoneNumber, string message) { if (string.IsNullOrEmpty(_token)) // ודא שהטוקן קיים { await AuthenticateAsync(); // אם לא, התחבר } // הוסף לוג לדיווח מצב הטוקן _logger.LogInformation($"Using token for call: {_token}"); var requestData = new { phone = phoneNumber, text = message }; var json = JsonConvert.SerializeObject(requestData); var content = new StringContent(json, Encoding.UTF8, "application/json"); // הוספת הטוקן לכותרת _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token); _logger.LogInformation($"Trying to make a call to {phoneNumber} with message: {message}"); _logger.LogInformation($"Request Data: {json}"); try { var response = await _httpClient.PostAsync($"https://private.call2all.co.il/ym/api/make-call", content); var responseContent = await response.Content.ReadAsStringAsync(); _logger.LogInformation($"Received response: {responseContent}"); if (!response.IsSuccessStatusCode) { _logger.LogError($"Request failed with status code {response.StatusCode}: {responseContent}"); return false; // או לזרוק שגיאה נוספת כאן } return true; // מחזיר true אם השיחה הושלמה בהצלחה } catch (Exception ex) { _logger.LogError(ex, "An error occurred while making a voice call."); return false; } // מתודה לשליחת שיחת טלפון //public async Task MakeVoiceCallAsync(string phoneNumber, string message) //{ // //var requestData = new //{ // username = "023132534", // password = "32534", // phone = phoneNumber, // text = message //}; //var json = JsonConvert.SerializeObject(requestData); //var content = new StringContent(json, Encoding.UTF8, "application/json"); //_logger.LogInformation($"Trying to make a call to {phoneNumber} with message: {message}"); //_logger.LogInformation($"Request Data: {json}"); //try //{ // var response = await _httpClient.PostAsync(ApiUrl, content); // var responseContent = await response.Content.ReadAsStringAsync(); // if (!response.IsSuccessStatusCode) // { // _logger.LogError($"Request failed with status code {response.StatusCode}: {responseContent}"); // } // return response.IsSuccessStatusCode; //} //catch (Exception ex) //{ // _logger.LogError(ex, "An error occurred while making a voice call."); // return false; //} //} } // מחלקת תוצאה עבור טוקן public class TokenResponse { public string Token { get; set; } } }