Пример #1
0
        private static void Main(string[] args)
        {
            LoadSession();
            var tmpEncoding = Encoding.UTF8;

            System.Console.OutputEncoding = Encoding.UTF8;
            System.Console.InputEncoding  = Encoding.UTF8;
            string nickname = "stico";

            string password = "******"; //v2 password
            string target   = "34629171696";                  // Mobile number to send the message to

            password = WhatsAppApi.Register.WhatsRegisterV2.RequestExist(sender);

            wa = new WhatsApp(sender, password, nickname, false);

            //event bindings
            wa.OnLoginSuccess             += wa_OnLoginSuccess;
            wa.OnLoginFailed              += wa_OnLoginFailed;
            wa.OnGetMessage               += wa_OnGetMessage;
            wa.OnGetMessageReadedClient   += wa_OnGetMessageReadedClient;
            wa.OnGetMessageReceivedClient += wa_OnGetMessageReceivedClient;
            wa.OnGetMessageReceivedServer += wa_OnGetMessageReceivedServer;
            wa.OnNotificationPicture      += wa_OnNotificationPicture;
            wa.OnGetPresence              += wa_OnGetPresence;
            wa.OnGetGroupParticipants     += wa_OnGetGroupParticipants;
            wa.OnGetLastSeen              += wa_OnGetLastSeen;
            wa.OnGetTyping                     += wa_OnGetTyping;
            wa.OnGetPaused                     += wa_OnGetPaused;
            wa.OnGetMessageImage               += wa_OnGetMessageImage;
            wa.OnGetMessageAudio               += wa_OnGetMessageAudio;
            wa.OnGetMessageVideo               += wa_OnGetMessageVideo;
            wa.OnGetMessageLocation            += wa_OnGetMessageLocation;
            wa.OnGetMessageVcard               += wa_OnGetMessageVcard;
            wa.OnGetPhoto                      += wa_OnGetPhoto;
            wa.OnGetPhotoPreview               += wa_OnGetPhotoPreview;
            wa.OnGetGroups                     += wa_OnGetGroups;
            wa.OnGetSyncResult                 += wa_OnGetSyncResult;
            wa.OnGetStatus                     += wa_OnGetStatus;
            wa.OnGetPrivacySettings            += wa_OnGetPrivacySettings;
            DebugAdapter.Instance.OnPrintDebug += Instance_OnPrintDebug;
            wa.SendGetServerProperties();
            //ISessionStore AxolotlStore
            wa.OnstoreSession         += wa_OnstoreSession;
            wa.OnloadSession          += wa_OnloadSession;
            wa.OngetSubDeviceSessions += wa_OngetSubDeviceSessions;
            wa.OncontainsSession      += wa_OncontainsSession;
            wa.OndeleteSession        += wa_OndeleteSession;
            // IPreKeyStore AxolotlStore
            wa.OnstorePreKey    += wa_OnstorePreKey;
            wa.OnloadPreKey     += wa_OnloadPreKey;
            wa.OnloadPreKeys    += wa_OnloadPreKeys;
            wa.OncontainsPreKey += wa_OncontainsPreKey;
            wa.OnremovePreKey   += wa_OnremovePreKey;

            //Sender key
            wa.OnstoreSenderKey    += wa_OnstoreSenderKey;
            wa.OnloadSenderKey     += wa_OnloadSenderKey;
            wa.OnremoveSenderKey   += wa_OnremoveSenderKey;
            wa.OncontainsSenderKey += wa_OncontainsSenderKey;

            // ISignedPreKeyStore AxolotlStore
            wa.OnstoreSignedPreKey    += wa_OnstoreSignedPreKey;
            wa.OnloadSignedPreKey     += wa_OnloadSignedPreKey;
            wa.OnloadSignedPreKeys    += wa_OnloadSignedPreKeys;
            wa.OncontainsSignedPreKey += wa_OncontainsSignedPreKey;
            wa.OnremoveSignedPreKey   += wa_OnremoveSignedPreKey;
            // IIdentityKeyStore AxolotlStore
            wa.OngetIdentityKeyPair     += wa_OngetIdentityKeyPair;
            wa.OngetLocalRegistrationId += wa_OngetLocalRegistrationId;
            wa.OnisTrustedIdentity      += wa_OnisTrustedIdentity;
            wa.OnsaveIdentity           += wa_OnsaveIdentity;
            wa.OnstoreLocalData         += wa_OnstoreLocalData;
            // Error Notification ErrorAxolotl
            wa.OnErrorAxolotl += wa_OnErrorAxolotl;

            wa.Connect();

            string datFile = getDatFileName(sender);

            byte[] nextChallenge = null;
            if (File.Exists(datFile))
            {
                try
                {
                    string foo = File.ReadAllText(datFile);
                    nextChallenge = Convert.FromBase64String(foo);
                }
                catch (Exception) { };
            }

            wa.Login(nextChallenge);
            wa.SendGetPrivacyList();
            wa.SendGetClientConfig();

            if (wa.LoadPreKeys() == null)
            {
                wa.sendSetPreKeys(true);
            }

            ProcessChat(wa, target);
            Console.ReadKey();
        }
Пример #2
0
        private static void Main(string[] args)
        {
            var tmpEncoding = Encoding.UTF8;

            Console.OutputEncoding = Encoding.Default;
            Console.InputEncoding  = Encoding.Default;
            string nickname = "QA Tim";
            string sender   = "553193087883";                 // Mobile number with country code (but without + or 00)
            string password = "******"; //v2 password
            string target   = "553188717292";                 // Mobile number to send the message to

            wa = new WhatsApp(sender, password, nickname, true);

            //event bindings
            wa.OnLoginSuccess             += wa_OnLoginSuccess;
            wa.OnLoginFailed              += wa_OnLoginFailed;
            wa.OnGetMessage               += wa_OnGetMessage;
            wa.OnGetMessageReadedClient   += wa_OnGetMessageReadedClient;
            wa.OnGetMessageReceivedClient += wa_OnGetMessageReceivedClient;
            wa.OnGetMessageReceivedServer += wa_OnGetMessageReceivedServer;
            wa.OnNotificationPicture      += wa_OnNotificationPicture;
            wa.OnGetPresence              += wa_OnGetPresence;
            wa.OnGetGroupParticipants     += wa_OnGetGroupParticipants;
            wa.OnGetLastSeen              += wa_OnGetLastSeen;
            wa.OnGetTyping                     += wa_OnGetTyping;
            wa.OnGetPaused                     += wa_OnGetPaused;
            wa.OnGetMessageImage               += wa_OnGetMessageImage;
            wa.OnGetMessageAudio               += wa_OnGetMessageAudio;
            wa.OnGetMessageVideo               += wa_OnGetMessageVideo;
            wa.OnGetMessageLocation            += wa_OnGetMessageLocation;
            wa.OnGetMessageVcard               += wa_OnGetMessageVcard;
            wa.OnGetPhoto                      += wa_OnGetPhoto;
            wa.OnGetPhotoPreview               += wa_OnGetPhotoPreview;
            wa.OnGetGroups                     += wa_OnGetGroups;
            wa.OnGetSyncResult                 += wa_OnGetSyncResult;
            wa.OnGetStatus                     += wa_OnGetStatus;
            wa.OnGetPrivacySettings            += wa_OnGetPrivacySettings;
            DebugAdapter.Instance.OnPrintDebug += Instance_OnPrintDebug;
            wa.SendGetServerProperties();
            //ISessionStore AxolotlStore
            wa.OnstoreSession         += wa_OnstoreSession;
            wa.OnloadSession          += wa_OnloadSession;
            wa.OngetSubDeviceSessions += wa_OngetSubDeviceSessions;
            wa.OncontainsSession      += wa_OncontainsSession;
            wa.OndeleteSession        += wa_OndeleteSession;
            // IPreKeyStore AxolotlStore
            wa.OnstorePreKey    += wa_OnstorePreKey;
            wa.OnloadPreKey     += wa_OnloadPreKey;
            wa.OnloadPreKeys    += wa_OnloadPreKeys;
            wa.OncontainsPreKey += wa_OncontainsPreKey;
            wa.OnremovePreKey   += wa_OnremovePreKey;
            // ISignedPreKeyStore AxolotlStore
            wa.OnstoreSignedPreKey    += wa_OnstoreSignedPreKey;
            wa.OnloadSignedPreKey     += wa_OnloadSignedPreKey;
            wa.OnloadSignedPreKeys    += wa_OnloadSignedPreKeys;
            wa.OncontainsSignedPreKey += wa_OncontainsSignedPreKey;
            wa.OnremoveSignedPreKey   += wa_OnremoveSignedPreKey;
            // IIdentityKeyStore AxolotlStore
            wa.OngetIdentityKeyPair     += wa_OngetIdentityKeyPair;
            wa.OngetLocalRegistrationId += wa_OngetLocalRegistrationId;
            wa.OnisTrustedIdentity      += wa_OnisTrustedIdentity;
            wa.OnsaveIdentity           += wa_OnsaveIdentity;
            wa.OnstoreLocalData         += wa_OnstoreLocalData;
            // Error Notification ErrorAxolotl
            wa.OnErrorAxolotl += wa_OnErrorAxolotl;

            wa.Connect();

            string datFile = getDatFileName(sender);

            byte[] nextChallenge = null;
            if (File.Exists(datFile))
            {
                try
                {
                    string foo = File.ReadAllText(datFile);
                    nextChallenge = Convert.FromBase64String(foo);
                }
                catch (Exception) { };
            }

            wa.Login(nextChallenge);
            wa.SendGetPrivacyList();
            wa.SendGetClientConfig();

            if (wa.LoadPreKeys() == null)
            {
                wa.sendSetPreKeys(true);
            }

            ProcessChat(wa, target);
            Console.ReadKey();
        }
Пример #3
0
        public void Start()
        {
            // registration if necessary
            string password = WhatsAppApi.Register.WhatsRegisterV2.RequestExist(PHONE);

            if (String.IsNullOrEmpty(password))
            {
                // register process
                string response = string.Empty;
                if (WhatsAppApi.Register.WhatsRegisterV2.RequestCode(PHONE, out password, out response, "sms"))
                {
                    Console.WriteLine("Response: [ " + response + " ]");
                    if (string.IsNullOrEmpty(password))
                    {
                        Console.Write("SMS sent. Enter code: ");
                        string code = Console.ReadLine();
                        password = WhatsAppApi.Register.WhatsRegisterV2.RegisterCode(PHONE, code, out response);
                        Console.WriteLine("Response: [ " + response + " ]");
                    }
                }
                else
                {
                    Console.WriteLine("Response: [ " + response + " ]");

                    Console.WriteLine("Trying to register by voice.");
                    if (WhatsAppApi.Register.WhatsRegisterV2.RequestCode(PHONE, out password, out response, "voice"))
                    {
                        Console.WriteLine("Response: [ " + response + " ]");
                        if (string.IsNullOrEmpty(password))
                        {
                            Console.Write("WhatsApp making a call. Enter code: ");
                            string code = Console.ReadLine();
                            password = WhatsAppApi.Register.WhatsRegisterV2.RegisterCode(PHONE, code, out response);
                            Console.WriteLine("Response: [ " + response + " ]");
                        }
                        else
                        {
                            Console.WriteLine("Cannot initiate register process (banned?)");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Response: [ " + response + " ]");
                    }
                }
            }

            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("Cannot complete register process (invalid code / banned)");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                Environment.Exit(-1);
            }

            waClient = new WhatsApp(PHONE, password, "nickname", false);

            bool registered = File.Exists(datFilename);

            //WhatsApp.DEBUG = true;
            //WhatsApp.DEBUGOutBound = true;
            DebugAdapter.Instance.OnPrintDebug += (data) =>
            {
                Console.WriteLine(data);
            };

            waClient.OnError += (id, from, code, text) =>
            {
                Console.WriteLine("ERROR: id={0}, from={1}, code={2}, text={3}", id, from, code, text);
            };

            waClient.OnConnectSuccess += () =>
            {
                Console.WriteLine("Connected success");
            };

            waClient.OnConnectFailed += (ex) =>
            {
                Console.WriteLine("Connected failed: " + ex.Message);
            };

            waClient.OnLoginSuccess += (pn, data) =>
            {
                loggedIn = true;

                string sdata = Convert.ToBase64String(data);
                Console.WriteLine("Login success. Next token: {0}", sdata);
                try
                {
                    File.WriteAllText(datFilename, sdata);
                }
                catch (Exception) { }

                waClient.SendGetPrivacyList();
                waClient.SendGetClientConfig();

                if (waClient.LoadPreKeys() == null)
                {
                    waClient.sendSetPreKeys(true);
                }

                threadReceiver = new Thread(t =>
                {
                    while (waClient != null)
                    {
                        waClient.PollMessages();
                        Thread.Sleep(100);
                    }
                })
                {
                    IsBackground = true
                };

                threadReceiver.Start();
            };

            waClient.OnLoginFailed += (data) => {
                Console.WriteLine("Login failed. Reason: {0}", data);
            };

            waClient.Connect();

            byte[] nextChallenge = null;

            if (registered)
            {
                try
                {
                    string foo = File.ReadAllText(datFilename);
                    nextChallenge = Convert.FromBase64String(foo);
                }
                catch (Exception) { };
            }

            waClient.Login(nextChallenge);
        }