Exemplo n.º 1
0
        public MesageItem ClearfyContent(MesageItem item)
        {
            try
            {
                string content = item.body.content;
                content = content.Replace("<div>", "");
                content = content.Replace("</div>", "");
                //content = content.Replace("<at id=\"0\">", "<at id=\\\"0\\\">");
                content = content.Replace("\"", "\\\"");
                string date = item.createdDateTime;
                date = Regex.Replace(date, @"\.[0-9]*Z", string.Empty);
                date = date.Replace("T", " ");
                string realdate = TimeZone.CurrentTimeZone.ToLocalTime(Convert.ToDateTime(date)).ToString();

                content = "[Created at: " + realdate + "] " + " " + content;
                // content = "[Created at: " + date + "]" + content;
                item.body.content = content;
                return(item);
            }
            catch (Exception e)
            {
                MessageBox.Show("There are some error while modefy the content, " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(null);
            }
        }
Exemplo n.º 2
0
        public string GetUserEmail(string AccessToken, MesageItem item)
        {
            try
            {
                string         UID              = item.From.user.id;
                HttpWebRequest request          = WebRequest.Create("https://graph.microsoft.com/v1.0/users/" + UID) as HttpWebRequest;
                var            myHttpWebRequest = (HttpWebRequest)request;
                request.Method = "GET";
                myHttpWebRequest.PreAuthenticate = true;
                myHttpWebRequest.Headers.Add("Authorization", "Bearer " + AccessToken);
                myHttpWebRequest.Accept = "application/json";

                var myWebResponse = request.GetResponse();

                var responseStream = myWebResponse.GetResponseStream();

                string objText = null;

                using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.UTF8))
                {
                    //while (myStreamReader.Peek()>=0) {

                    objText = myStreamReader.ReadToEnd();
                    //Console.WriteLine(objText);
                    //}
                }


                JavaScriptSerializer js = new JavaScriptSerializer();

                var    myojb = (UserInfoItem)js.Deserialize(objText, typeof(UserInfoItem));
                string email = myojb.mail;


                responseStream.Close();
                myWebResponse.Close();
                return(email);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(null);
            }
        }
Exemplo n.º 3
0
        private Task testFuctions(string fid, string c_id, string ttid, string tcid, int map_id)
        {
            return(Task.Run(async() =>
            {
                string stacd = "2";
                string token7 = GetToken(stacd);

                List <MapUser> ue = GetUsrMap();
                List <MapMenUser> ftemail = new List <MapMenUser>();;
                MigrationStep step3 = new MigrationStep();
                if (ue.Count > 0 & ue != null)
                {
                    string stacdc = "1";
                    string token4 = GetToken(stacdc);
                    string stacd1 = "2";
                    string token3 = GetToken(stacd1);
                    for (int i = 0; i < ue.Count; i++)
                    {
                        MapMenUser ftuser = new MapMenUser();
                        UserInfoItem fuser = step3.GetUserByEmail(token3, ue[i].femail);
                        UserInfoItem tuser = step3.GetUserByEmail(token4, ue[i].temail);
                        ftuser.fid = fuser.id;
                        ftuser.fdisname = fuser.displayName;
                        ftuser.femail = fuser.mail;
                        ftuser.tid = tuser.id;
                        ftuser.tdisname = tuser.displayName;
                        ftuser.temail = tuser.mail;

                        ftemail.Add(ftuser);
                    }
                }

                if (!String.IsNullOrEmpty(token7))
                {
                    GetChatContent step2 = new GetChatContent();
                    List <MesageItem> messages = step2.GetChanelMessages(token7, fid, c_id);
                    Comparison <MesageItem> comparison = new Comparison <MesageItem>((MesageItem x, MesageItem y) =>
                    {
                        if (x.id < y.id)
                        {
                            return -1;
                        }
                        else if (x.id == y.id)
                        {
                            return 0;
                        }
                        else
                        {
                            return 1;
                        }
                    });
                    messages.Sort(comparison);

                    for (int i = 0; i < messages.Count; i++)
                    {
                        string user1 = step2.GetUserEmail(token7, messages[i]);
                        string usr1 = GetUsrMapemail(user1);
                        string token3 = GetUsrToken(usr1);

                        string oneaccess = "";
                        if (String.IsNullOrEmpty(token3))
                        {
                            string sta_cd = "1";
                            con.Open();
                            SqlCommand cmd2 = new SqlCommand("select_token", con);
                            cmd2.CommandType = CommandType.StoredProcedure;
                            cmd2.Parameters.AddWithValue("@sta_cd", sta_cd);
                            using (SqlDataReader reader = cmd2.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    token1 = reader["access_token"].ToString();
                                    ppwd = reader["p_pwd"].ToString();
                                    cid = reader["c_id"].ToString();
                                    cs = reader["c_cs"].ToString();
                                    tid = reader["t_id"].ToString();
                                }
                            }
                            con.Close();
                            oneaccess = step3.GetOneAccessToken(usr1, ppwd, cid, cs, tid);
                        }
                        else
                        {
                            oneaccess = token3;
                        }

                        MesageItem mesaft = step3.ClearfyContent(messages[i]);

                        var res = step2.SendMessageDe(oneaccess, ttid, tcid, mesaft, ftemail);
                        if (res == null)
                        {
                            System.Windows.Forms.MessageBox.Show("One message throw an error");
                            continue;
                        }

                        long new_m_id = res.id;

                        List <MesageItem> replies = step2.GetMessagesReply(token7, fid, c_id, messages[i].id.ToString());

                        if (replies.Count > 0)
                        {
                            Comparison <MesageItem> comparison1 = new Comparison <MesageItem>((MesageItem x, MesageItem y) =>
                            {
                                if (x.id < y.id)
                                {
                                    return -1;
                                }
                                else if (x.id == y.id)
                                {
                                    return 0;
                                }
                                else
                                {
                                    return 1;
                                }
                            });
                            replies.Sort(comparison1);

                            foreach (MesageItem reply in replies)
                            {
                                MesageItem newreply = step3.ClearfyContent(reply);
                                long mesid = new_m_id;
                                string user2 = step2.GetUserEmail(token7, reply);
                                string usr2 = GetUsrMapemail(user2);
                                string token6 = GetUsrToken(usr2);

                                string oaccess = "";
                                if (String.IsNullOrEmpty(token6))
                                {
                                    string sta_cd = "1";
                                    con.Open();
                                    SqlCommand cmd2 = new SqlCommand("select_token", con);
                                    cmd2.CommandType = CommandType.StoredProcedure;
                                    cmd2.Parameters.AddWithValue("@sta_cd", sta_cd);
                                    using (SqlDataReader reader = cmd2.ExecuteReader())
                                    {
                                        if (reader.Read())
                                        {
                                            token1 = reader["access_token"].ToString();
                                            ppwd = reader["p_pwd"].ToString();
                                            cid = reader["c_id"].ToString();
                                            cs = reader["c_cs"].ToString();
                                            tid = reader["t_id"].ToString();
                                        }
                                    }
                                    con.Close();
                                    oaccess = step3.GetOneAccessToken(usr2, ppwd, cid, cs, tid);
                                }
                                else
                                {
                                    oaccess = token6;
                                }
                                if (ftemail.Count > 0 & ftemail != null)
                                {
                                    if (!step2.ReplyMessageDe(oaccess, ttid, tcid, mesid, newreply, ftemail))
                                    {
                                        System.Windows.Forms.MessageBox.Show("Send a reply error");
                                        continue;
                                    }
                                }
                                else
                                {
                                    System.Windows.Forms.MessageBox.Show("Access token expired, please sign in again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    Thread.Sleep(200);
                                    var account = await App.PublicClientApp.GetAccountsAsync();
                                    if (account.Any())
                                    {
                                        try
                                        {
                                            await App.PublicClientApp.RemoveAsync(account.FirstOrDefault());
                                            con.Open();
                                            SqlCommand cmd1 = new SqlCommand("user_del", con);
                                            cmd1.CommandType = CommandType.StoredProcedure;
                                            cmd1.ExecuteNonQuery();
                                            con.Close();


                                            ThirdPage th = new ThirdPage();
                                            th.Show();
                                            this.Hide();
                                        }
                                        catch (MsalException ex)
                                        {
                                            System.Windows.Forms.MessageBox.Show(ex.Message);
                                        }
                                    }
                                    else
                                    {
                                        ThirdPage th = new ThirdPage();
                                        th.Show();
                                        this.Hide();
                                    }
                                    break;
                                }
                            }
                        }
                        int c = i + 1;
                        SetprogressBar1(c, messages.Count);
                        Thread.Sleep(5);
                    }
                    con.Open();
                    SqlCommand cmd6 = new SqlCommand("upt_map", con);
                    cmd6.CommandType = CommandType.StoredProcedure;
                    cmd6.Parameters.AddWithValue("@map_id", map_id);
                    cmd6.Parameters.AddWithValue("@cnt_mes", "");
                    cmd6.ExecuteNonQuery();
                    con.Close();
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Access token expired, please sign in again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Thread.Sleep(200);
                    var account = await App.PublicClientApp.GetAccountsAsync();
                    if (account.Any())
                    {
                        try
                        {
                            await App.PublicClientApp.RemoveAsync(account.FirstOrDefault());
                            con.Open();
                            SqlCommand cmd1 = new SqlCommand("user_del", con);
                            cmd1.CommandType = CommandType.StoredProcedure;
                            cmd1.ExecuteNonQuery();
                            con.Close();


                            ThirdPage th = new ThirdPage();
                            th.Show();
                            this.Hide();
                        }
                        catch (MsalException ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        ThirdPage th = new ThirdPage();
                        th.Show();
                        this.Hide();
                    }
                }
            }));
        }
Exemplo n.º 4
0
        public Boolean ReplyMessageDe(string AccessToken, string TeamID, string ChannelID, long MessageID, MesageItem item, List <MapMenUser> ftemail)
        {
            try
            {
                HttpWebRequest request          = WebRequest.Create("https://graph.microsoft.com/beta/teams/" + TeamID + "/channels/" + ChannelID + "/messages/" + MessageID + "/replies") as HttpWebRequest;
                var            myHttpWebRequest = (HttpWebRequest)request;
                request.Method = "POST";
                myHttpWebRequest.ContentType     = "application/json";
                myHttpWebRequest.PreAuthenticate = true;
                myHttpWebRequest.Headers.Add("Authorization", "Bearer " + AccessToken);
                string content = item.body.content;
                string mention = " \"mentions\": [";
                foreach (var men in item.mentions)
                {
                    var keyValue = ftemail.FirstOrDefault(x => x.fid == men.mentioned.user.id);
                    //search email
                    string uid     = "";
                    string udsname = "";
                    if (keyValue != null)
                    {
                        uid     = keyValue.tid;
                        udsname = keyValue.tdisname;
                        content = content.Replace(men.mentionText, udsname);
                    }
                    else
                    {
                        MessageBox.Show("No Mapping User Found");
                        return(false);
                    }
                    mention = mention + "{\"id\":" + men.id + ",\"mentionText\":\"" + udsname + "\"," + "\"mentioned\": { \"user\": {\"displayName\":\"" + udsname + "\",\"id\": \"" + uid + "\",\"userIdentityType\": \"" + men.mentioned.user.userIdentityType + "\"}}}";
                }
                mention += "]";

                //MessageBox.Show(item.body.contentType + item.body.content);
                //MessageBox.Show(mention);
                using (var streamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream()))
                {
                    string json = "{\"body\":{\"contentType\":\"" + item.body.contentType + "\"," +
                                  "\"content\":\"" + content + "\"}," + mention + "}";

                    /*
                     * string json = "{\"body\":{\"contentType\":\"" + item.body.contentType + "\"," +
                     *         "\"content\":\"" + item.body.content + "\"}}";
                     */
                    //MessageBox.Show(json);
                    streamWriter.Write(json);
                }
                var httpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
                return(true);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return(false);
            }
        }
Exemplo n.º 5
0
        public Boolean ReplyMessage(string AccessToken, string TeamID, string ChannelID, long MessageID, MesageItem item)
        {
            try
            {
                HttpWebRequest request          = WebRequest.Create("https://graph.microsoft.com/beta/teams/" + TeamID + "/channels/" + ChannelID + "/messages/" + MessageID + "/replies") as HttpWebRequest;
                var            myHttpWebRequest = (HttpWebRequest)request;
                request.Method = "POST";
                myHttpWebRequest.ContentType     = "application/json";
                myHttpWebRequest.PreAuthenticate = true;
                myHttpWebRequest.Headers.Add("Authorization", "Bearer " + AccessToken);

                string mention = " \"mentions\": [";
                foreach (var men in item.mentions)
                {
                    mention = mention + "{\"id\":" + men.id + ",\"mentionText\":\"" + men.mentionText + "\"," + "\"mentioned\": { \"user\": {\"displayName\":\"" + men.mentioned.user.displayName + "\",\"id\": \"" + men.mentioned.user.id + "\",\"userIdentityType\": \"" + men.mentioned.user.userIdentityType + "\"}}}";
                }
                mention += "]";

                //MessageBox.Show(item.body.contentType + item.body.content);
                //MessageBox.Show(mention);
                using (var streamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream()))
                {
                    string json = "{\"body\":{\"contentType\":\"" + item.body.contentType + "\"," +
                                  "\"content\":\"" + item.body.content + "\"}," + mention + "}";

                    /*
                     * string json = "{\"body\":{\"contentType\":\"" + item.body.contentType + "\"," +
                     *         "\"content\":\"" + item.body.content + "\"}}";
                     */
                    //MessageBox.Show(json);
                    streamWriter.Write(json);
                }
                var httpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
                return(true);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(false);
            }
        }