private void BackgroundWorker_GetUpdate_DoWork(object sender, DoWorkEventArgs e) { while (true) { if (BackgroundWorker_GetUpdate.CancellationPending) { e.Cancel = true; BotStart_Botton.Invoke((Action)(() => BotStart_Botton.BackColor = Color.Orange)); BotStart_Botton.Invoke((Action)(() => BotStart_Botton.Text = "اتصال ")); BotStart_Botton.Invoke((Action)(() => BotStart_Botton.Enabled = true)); break; } try { string last_index = ctrl.read_index(); T_result new_massage = ctrl.get_update(last_index); BotStart_Botton.Invoke((Action)(() => BotStart_Botton.BackColor = Color.Green)); BotStart_Botton.Invoke((Action)(() => BotStart_Botton.Text = "متصل شد ")); if (new_massage.result.Count > 0) { Bot_Log(new_massage); ctrl.responce(new_massage); ctrl.save_index(ctrl.get_update()); } } catch (Exception ex) { BotStart_Botton.Invoke((Action)(() => BotStart_Botton.Enabled = true)); BotStart_Botton.Invoke((Action)(() => BotStart_Botton.BackColor = Color.Tomato)); BotStart_Botton.Invoke((Action)(() => BotStart_Botton.Text = "... عدم اتصال ، تلاش برای اتصال مجدد")); warning_log(ex.Message); } } }
private void Bot_Log(T_result NewUpdates) { foreach (var item in NewUpdates.result) { Data_log(item); } }
private void UpdateChecker_Timer_Tick(object sender, EventArgs e) { try { string last_index = ctrl.read_index(); T_result new_massage = ctrl.get_update(last_index); BotStart_Botton.BackColor = Color.Green; BotStart_Botton.Text = "متصل شد "; if (new_massage.result.Count > 0) { Bot_Log(new_massage); ctrl.responce(new_massage); ctrl.save_index(ctrl.get_update()); } } catch (Exception ex) { UpdateChecker_Timer.Stop(); ReStartBot_timer.Start(); BotStart_Botton.BackColor = Color.Orange; BotStart_Botton.Enabled = true; BotStart_Botton.Text = "عدم اتصال"; warning_log(ex.Message); } }
//save latest index of array public void save_index(T_result UpdateList) { int index = UpdateList.result.Count; List <Update> _update = UpdateList.result; string update_id = (_update[index - 1].update_id + 1).ToString(); File.WriteAllText("index", update_id); }
public T_result get_update(string offset) { string url = string.Format(this.BaseUrl + "getupdates?offset={0}", offset); string responce = create_requst(url); T_result update_list = JsonConvert.DeserializeObject <T_result>(responce); return(update_list); }
public T_result get_update() { string url = this.BaseUrl + "getupdates"; string responce = create_requst(url); T_result update_list = JsonConvert.DeserializeObject <T_result>(responce); return(update_list); }
public void responce(T_result t_Result) { foreach (var item in t_Result.result) { message msg = item.message; User from = msg.from; string msgtext = msg.text; int user_id = from.id; Decision(msgtext, user_id); } }
static void Main(string[] args) { Controlls.SSL(); Controlls ctrl = new Controlls();//bot controller Console.WriteLine("Server Started "); while (true) { string last_index = ctrl.read_index(); T_result new_massage = ctrl.get_update(last_index); if (new_massage.result.Count > 0) { ctrl.responce(new_massage); ctrl.save_index(ctrl.get_update()); } } }