コード例 #1
0
 private void send()
 {
     try
     {
         WhatsUserManager manager = new WhatsUserManager();
         user = manager.CreateUser(textBox5MyMobile.Text, textBox4Name.Text);
         var thread = new Thread(t =>
         {
             UpdateTextBox textbox = UpdateDataTextBox;
             wa = new WhatsApp(textBox5MyMobile.Text, textBox6Password.Text, textBox4Name.Text, true);
             wa.OnConnectSuccess += () =>
             {
                 if (textBox3Status.InvokeRequired)
                 {
                     Invoke(textbox, textBox3Status, "Connected...");
                 }
                 wa.OnLoginSuccess += (phone, data) =>
                 {
                     if (textBox3Status.InvokeRequired)
                     {
                         Invoke(textbox, textBox3Status, "\r\nLogin success!");
                     }
                     while (wa != null)
                     {
                         wa.PollMessages();
                         Thread.Sleep(200);
                         continue;
                     }
                 };
                 wa.OnGetMessage += (node, from, id, name, message, receipt_sent) =>
                 {
                     if (textBox3Status.InvokeRequired)
                     {
                         Invoke(textbox, textBox3Status, string.Format("\r\n{0}:{1}", name, message));
                     }
                 };
                 wa.OnLoginFailed += (data) =>
                 {
                     if (textBox3Status.InvokeRequired)
                     {
                         Invoke(textbox, textBox3Status, string.Format("\r\nLogin failed:{0}", data));
                     }
                 };
                 wa.Login();
             };
             wa.OnConnectFailed += (ex) =>
             {
                 if (textBox3Status.InvokeRequired)
                 {
                     Invoke(textbox, textBox3Status, string.Format("\r\nConnect failed: {0}", ex.StackTrace));
                 }
             };
             wa.Connect();
         })
         {
             IsBackground = true
         };
         thread.Start();
     }
     catch (Exception ex) { MessageBox.Show("Connection failed. Check input data, remove polish letters, to telephone number add prefix 48" + ex); }
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: wasimjee/AxolotlTestApp
        private static void ProcessChat(WhatsApp wa, string dst)
        {
            var thRecv = new Thread(t =>
            {
                try
                {
                    while (wa != null)
                    {
                        wa.PollMessages();
                        Thread.Sleep(100);
                        continue;
                    }
                }
                catch (ThreadAbortException)
                {
                }
            })
            {
                IsBackground = true
            };

            thRecv.Start();

            WhatsUserManager usrMan = new WhatsUserManager();
            var tmpUser             = usrMan.CreateUser(dst, "User");

            while (true)
            {
                string line = Console.ReadLine();
                if (line == null && line.Length == 0)
                {
                    continue;
                }

                string command = line.Trim();
                switch (command)
                {
                case "/query":
                    //var dst = dst//trim(strstr($line, ' ', FALSE));
                    Console.WriteLine("[] Interactive conversation with {0}:", tmpUser);
                    break;

                case "/accountinfo":
                    Console.WriteLine("[] Account Info: {0}", wa.GetAccountInfo().ToString());
                    break;

                case "/lastseen":
                    Console.WriteLine("[] Request last seen {0}", tmpUser);
                    wa.SendQueryLastOnline(tmpUser.GetFullJid());
                    break;

                case "/exit":
                    wa = null;
                    thRecv.Abort();
                    return;

                case "/start":
                    wa.SendComposing(tmpUser.GetFullJid());
                    break;

                case "/pause":
                    wa.SendPaused(tmpUser.GetFullJid());
                    break;

                default:
                    Console.WriteLine("[] Send message to {0}: {1}", tmpUser, line);
                    wa.SendMessage(tmpUser.GetFullJid(), line);
                    break;
                }
            }
        }
コード例 #3
0
        protected void Button2_Click(object sender, EventArgs e)
        {
            WhatsUserManager Manage = new WhatsUserManager();

            USER = Manage.CreateUser(TextBox5.Text, TextBox6.Text);
            var thread = new Thread(testc =>
            {
                UpdateTextBox textbox = UpdateDataTextBox;
                WA = new Whatsapp(TextBox5.Text, TextBox6.Text, TextBox4.Text, true);
                WA.OnConnectSuccess += () =>
                {
                    if (TextBox3.InvokeRequired)
                    {
                        Invoke(textbox, TextBox3, "Connected....");
                    }
                    WA.OnLoginSuccess += (phone, data) =>
                    {
                        if (TextBox3.InvokeRequired)
                        {
                            invoke(textbox, TextBox3, "\r\nLogin Success....");
                        }
                        while (WA != null)
                        {
                            WA = PollMessage();
                            Thread.Sleep(200);
                            continue;
                        }
                    };
                    WA.OnGetMessage += (node, from, id, name, message, receipt_send) =>
                    {
                        if (TextBox3.InvokeRequired)
                        {
                            invoke(textbox, TextBox3, string.Format("\r\n{0}:{1}", name, message));
                        }
                    };
                    WA.OnLoginFailed += (data) =>
                    {
                        if (TextBox3.InvokeRequired)
                        {
                            invoke(textbox, TextBox3, string.Format("\r\nConnect failed: {0}", data));
                        }
                    };
                    WA.Login();
                };
                WA.OnConnectFailed += (ex) =>
                {
                    if (TextBox3.InvokeRequired)
                    {
                        invoke(textbox, TextBox3, string.Format("\r\nConnection Failed {0}", ex.StactTrace));
                    }
                };



                WA.Connect();
            })
            {
                IsBackground = true
            };

            thread.Start();
        }
コード例 #4
0
        private void OnReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
        {
            try
            {
                System.Messaging.Message msg = _queue.EndReceive(e.AsyncResult);
                FireRecieveEvent(msg);
                string[] strArr    = null;
                char[]   splitchar = { '|' };
                strArr = e.Message.Label.Split(splitchar);
                string folio, receptor, recurso;
                folio    = strArr[0];
                receptor = strArr[1];
                recurso  = strArr[2];
                bool nuevaConexion = false;
                if (e.Message.Priority == MessagePriority.High)
                {
volver_conectar:
                    try
                    {
                        SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusListoParaEnviar(Usuario, dtEmisores_UsuariosEN_1.Rows[0]["EMISOR"].ToString(), folio, "");
                        WhatsUserManager usrMan = new WhatsUserManager();
                        var tmpUser             = usrMan.CreateUser(receptor, "User" + receptor);
                        WhatsAppApi.Parser.FMessage.FMessageIdentifierKey key = new WhatsAppApi.Parser.FMessage.FMessageIdentifierKey(tmpUser.GetFullJid(), true, folio);
                        WhatsAppApi.Parser.FMessage msj = new WhatsAppApi.Parser.FMessage(key);
                        msj.data = e.Message.Body.ToString();
                        if (dtUltimaConexion.AddMinutes(1) < DateTime.Now | nuevaConexion)
                        {
                            wa_1.Connect();
                            wa_1.Login();
                        }
                        dtUltimaConexion = DateTime.Now;
                        if (wa_1.ConnectionStatus == ApiBase.CONNECTION_STATUS.CONNECTED | wa_1.ConnectionStatus == ApiBase.CONNECTION_STATUS.DISCONNECTED)
                        {
                            wa_1.Connect();
                            wa_1.Login();
                        }
                        else if (wa_1.ConnectionStatus == ApiBase.CONNECTION_STATUS.UNAUTHORIZED)
                        {
                            SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusErrorEnvio(Usuario, dtEmisores_UsuariosEN_1.Rows[0]["EMISOR"].ToString(), folio, "No Autorizado para enviar");
                            this.Stop();
                            return;
                        }
                        wa_1.SendMessage(msj);
                        Random objRandom = new Random();
                        System.Threading.Thread.Sleep(objRandom.Next(2000, 3000));
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "Se ha anulado una conexión establecida por el software en su equipo host")
                        {
                            nuevaConexion = true;
                            wa_1.Disconnect();
                            System.Threading.Thread.Sleep(5000);
                            goto volver_conectar;
                        }
                        else
                        {
                            SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusErrorEnvio(Usuario, dtEmisores_UsuariosEN_1.Rows[0]["EMISOR"].ToString(), folio, "Error al enviar: " + ex.Message);
                            return;
                        }
                    }
                    SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusEnviando(Usuario, dtEmisores_UsuariosEN_1.Rows[0]["EMISOR"].ToString(), folio, "");
                }
                else
                {
volver_conectar:
                    try
                    {
                        SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusListoParaEnviar(Usuario, dtEmisores_UsuariosEN.Rows[0]["EMISOR"].ToString(), folio, "");
                        WhatsUserManager usrMan = new WhatsUserManager();
                        var tmpUser             = usrMan.CreateUser(receptor, "User" + receptor);
                        WhatsAppApi.Parser.FMessage.FMessageIdentifierKey key = new WhatsAppApi.Parser.FMessage.FMessageIdentifierKey(tmpUser.GetFullJid(), true, folio);
                        WhatsAppApi.Parser.FMessage msj = new WhatsAppApi.Parser.FMessage(key);
                        msj.data = e.Message.Body.ToString();
                        if (dtUltimaConexion.AddMinutes(1) < DateTime.Now | nuevaConexion)
                        {
                            wa.Connect();
                            wa.Login();
                        }
                        dtUltimaConexion = DateTime.Now;
                        if (wa.ConnectionStatus == ApiBase.CONNECTION_STATUS.CONNECTED | wa.ConnectionStatus == ApiBase.CONNECTION_STATUS.DISCONNECTED)
                        {
                            wa.Connect();
                            wa.Login();
                        }
                        else if (wa.ConnectionStatus == ApiBase.CONNECTION_STATUS.UNAUTHORIZED)
                        {
                            SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusErrorEnvio(Usuario, dtEmisores_UsuariosEN.Rows[0]["EMISOR"].ToString(), folio, "No Autorizado para enviar");
                            this.Stop();
                            return;
                        }
                        wa.SendMessage(msj);
                        Random objRandom = new Random();
                        System.Threading.Thread.Sleep(objRandom.Next(2000, 3000));
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "Se ha anulado una conexión establecida por el software en su equipo host")
                        {
                            nuevaConexion = true;
                            wa.Disconnect();
                            System.Threading.Thread.Sleep(5000);
                            goto volver_conectar;
                        }
                        else
                        {
                            SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusErrorEnvio(Usuario, dtEmisores_UsuariosEN.Rows[0]["EMISOR"].ToString(), folio, "Error al enviar: " + ex.Message);
                            return;
                        }
                    }
                    SchoolManager.WhatsApp.LogicaNegocios.WhatsApp_UsuarioLN.PonerStatusEnviando(Usuario, dtEmisores_UsuariosEN.Rows[0]["EMISOR"].ToString(), folio, "");
                }
                StartListening();
            }
            catch (Exception ex)
            {
                this.Stop();
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
コード例 #5
0
        private static void ProcessChat(WhatsApp _WhatsAppApi, string _Dest)
        {
            Thread thRecv = new Thread(t =>
            {
                try
                {
                    while (_WhatsAppApi != null)
                    {
                        _WhatsAppApi.PollMessages();
                        Thread.Sleep(100);
                        continue;
                    }
                }
                catch (ThreadAbortException)
                {
                }
            })
            {
                IsBackground = true
            };

            thRecv.Start();

            WhatsUserManager usrMan  = new WhatsUserManager();
            WhatsUser        tmpUser = usrMan.CreateUser(_Dest, "User");

            while (true)
            {
                String line = Console.ReadLine();
                if (String.IsNullOrEmpty(line))
                {
                    continue;
                }

                string command = line.Trim();
                switch (command)
                {
                case "/query":
                    Console.WriteLine("[] Interactive conversation with {0}:", tmpUser);
                    break;

                case "/accountinfo":
                    Console.WriteLine("[] Account Info: {0}", _WhatsAppApi.GetAccountInfo().ToString());
                    break;

                case "/lastseen":
                    Console.WriteLine("[] Request last seen {0}", tmpUser);
                    _WhatsAppApi.SendQueryLastOnline(tmpUser.GetFullJid());
                    break;

                case "/exit":
                    _WhatsAppApi = null;
                    thRecv.Abort();
                    return;

                case "/start":
                    _WhatsAppApi.SendComposing(tmpUser.GetFullJid());
                    break;

                case "/pause":
                    _WhatsAppApi.SendPaused(tmpUser.GetFullJid());
                    break;

                default:
                    Console.WriteLine("[] Send message to {0}: {1}", tmpUser, line);
                    _WhatsAppApi.SendMessage(tmpUser.GetFullJid(), line);
                    break;
                }
            }
        }