public async Task<BitrixNotifications> GetBxNotifications(string access_token) { var http = new HttpClientManager(); var bx = new BXManager(); var notifs = await http.GetAndDeserialize<BitrixNotifications>(bx.bxRestUrl + "im.counters.get?auth=" + access_token,"result=>TYPE"); return notifs; }
public JsonDocument AuthBitrix(string refreshToken, out int expires) { BXManager bxManager = new BXManager(); var x = bxManager.AuthBitrix(refreshToken); JsonDocument json; try { json = JsonDocument.Parse(x.Result.ToString()); } catch (Exception ex) { throw new UserExceptions("სერვისი დროებით მიუწვდომელია"); // + ex.Message); } JsonElement bxRefreshToken; JsonElement expiresJ; json.RootElement.TryGetProperty("refresh_token", out bxRefreshToken); json.RootElement.TryGetProperty("expires", out expiresJ); expiresJ.TryGetInt32(out expires); if (!new PKG_USERS().saveBxRefreshToken(bxRefreshToken.ToString(), authUser.ID)) // if fails to save { throw new Exception("refresh token ვერ შეინახა."); } return(json); }
public async Task <JsonDocument> refreshAndCheckNotifications([FromQuery] string key) { if (key == "5cdd1205f9569ef2d78b25f20eec53e7") // md5 from String(http://ants.ge/refreshTokens) { // BXManager bxManager = new BXManager(); // var x = bxManager.AuthBitrix(refreshToken); var users = new PKG_USERS().GetAllBxUsers(); foreach (var user in users) { try { BXManager bxManager = new BXManager(); var jsonToken = bxManager.AuthBitrix(user.bx_refresh_token); string json = null; json = jsonToken.Result.ToString(); if (string.IsNullOrEmpty(json)) { continue; } // if succeed deserialize var obj = new BitrixUserToken(); obj = JsonSerializer.Deserialize <BitrixUserToken>(json); // save if (obj.refresh_token.ToString().Length > 0) { new PKG_USERS().saveBxRefreshToken(obj.refresh_token.ToString(), user.ID); } // if succeed check for chat notifications var bxController = new BitrixController(); BitrixNotifications bxNotifs = new BitrixNotifications(); bxNotifs = await bxController.GetBxNotifications(obj.access_token); if (bxNotifs != null && (Convert.ToInt32(bxNotifs.CHAT) + Convert.ToInt32(bxNotifs.DIALOG)) > 0) { var emailTo = user.email; //emailTo = "*****@*****.**"; new EmailService().SendEmail(emailTo, "წაუკითხავი შეტყობინებები", "<b>თქვენ გაქვთ " + (Convert.ToInt32(bxNotifs.CHAT) + Convert.ToInt32(bxNotifs.DIALOG)) + " წაუკითხავი შეტყობინება.</b> გთხოვთ წაიკითხოთ https://crm.ants.ge/Chat"); } } catch (Exception ex) { // Does not automatically log errors since the program must not stop, it has to continue refreshing bx Refresh tokens. That's why i log manually here // AuthUser.ID = -1 means it's Job Error new PKG_ERROR_LOGS().LogException(-1, ex.Message, ex.StackTrace, "/AutomaticJob/refreshAndCheckNotifications", HttpContext.Connection.RemoteIpAddress.ToString(), HttpContext.Request.QueryString.ToString()); } } ; return(Success()); } else { return(throwError("invalid action")); } }
public void HandleBxEvent() { /// Bitrix Event Handles FORM Type parameters /// Request.Form[data[FIELDS_AFTER]] /// Request.Form[data[FIELDS_AFTER][ID]] /// Request.Form[auth[user_id]] /// etc... see documentation var form = HttpContext.Request.Form; var emailTo = ""; var emailSubject = ""; var emailText = ""; var taskId = "0"; if (form == null || form.Count == 0) return; var ev = form["event"]; switch (ev) { case "ONTASKADD": emailSubject = "ახალი ამოცანა"; emailText = "პორტალზე დაემატა ახალი ამოცანა <a href='https://crm.ants.ge/Tasks/" + form["data[FIELDS_AFTER][ID]"] + "'> ID: " + form["data[FIELDS_AFTER][ID]"] + "</a>"; taskId = form["data[FIELDS_AFTER][ID]"]; break; case "ONTASKCOMMENTADD": emailSubject = "ახალი კომენტარი"; emailText = "ამოცანას დაემატა კომენტარი. იხილეთ ამოცანა <a href='https://crm.ants.ge/Tasks/" + form["data[FIELDS_AFTER][TASK_ID]"] + "'> ID: " + form["data[FIELDS_AFTER][TASK_ID]"] + "</a>"; taskId = form["data[FIELDS_AFTER][TASK_ID]"]; break; } if (taskId == "0") throw new Exception("No Task ID"); var http = new HttpClient(); var bx = new BXManager(); var responseMessage = http.GetAsync(bx.bxRestWebHookUrl + "tasks.task.get?taskId=" + taskId).Result; JsonDocument json = null; if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK) { var response = responseMessage.Content.ReadAsStringAsync(); json = JsonDocument.Parse(response.Result); } var responsibleId = Convert.ToInt32(json.RootElement.GetProperty("result").GetProperty("task").GetProperty("responsibleId").ToString()); var pkg = new PKG_USERS(); var user = pkg.GetUser(responsibleId); emailTo = user.email; if (emailTo.Trim().Length == 0) throw new Exception("No emailTo Found"); new EmailService().SendEmail(emailTo, emailSubject, emailText); }
public async Task<bool> PermissionToWorkgroup(string accessToken, string workgroupName) { var bx = new BXManager(); var httpManager = new HttpClientManager(); var bxWorkgroups = await httpManager.GetAndDeserialize<List<BitrixWorkgroup>>(bx.bxRestUrl + "sonet_group.get?auth=" + accessToken + "&FILTER[NAME]=" + workgroupName, "result"); //var http = new HttpClient(); //HttpResponseMessage response = await http.GetAsync(bx.bxRestUrl + "sonet_group.get?auth=" + accessToken + "&FILTER[NAME]=" + workgroupName); //var json = JsonDocument.Parse(response.Content.ReadAsStringAsync().Result); //var bxWorkgroups = JsonSerializer.Deserialize<List<BitrixWorkgroup>>(Convert.ToString(json.RootElement.GetProperty("result"))); if (bxWorkgroups.Count > 0) return true; return false; }