コード例 #1
0
        void client_OnGetMessage(WhatsAppApi.Helper.ProtocolTreeNode messageNode, string from, string id, string name, string message, bool receipt_sent)
        {
            RosterItem rsRosterItem = Roster.Instance.Get(from);

            if (rsRosterItem == null)
            {
                rsRosterItem = new RosterItem()
                {
                    Jid      = from,
                    Name     = name,
                    Messages = new System.Collections.ObjectModel.ObservableCollection <Message>()
                };
                Roster.Instance.Add(rsRosterItem);

                client.SendGetPhoto(from, "", false);
                client.SendGetStatuses(new string[] { from });
            }

            rsRosterItem.Add(new Message()
            {
                Text        = message,
                Time        = DateTime.Now,
                ID          = id,
                FromJid     = from,
                ToJid       = "",
                MessageType = Enumerations.MessageType.Incoming
            });
        }
コード例 #2
0
        public ContactInfo GetInfoByPhone(string phone)
        {
            if (!loggedIn)
            {
                throw new InvalidOperationException("Not logged in.");
            }

            ContactInfo result = new ContactInfo();

            lock (this)
            {
                WhatsUserManager usrMan = new WhatsUserManager();
                var tmpUser             = usrMan.CreateUser(phone, "User");
                result.FullJid = tmpUser.GetFullJid();
                result.Phone   = "+" + phone;

                // get last seen time
                OnGetLastSeenDelegate onGetLastSeenHandler = (from, lastSeen) =>
                {
                    Console.WriteLine("{0} last seen on {1}", from, lastSeen.ToString());
                    result.LastSeen = lastSeen;
                };
                waClient.OnGetLastSeen += onGetLastSeenHandler;
                Console.WriteLine("[] Request last seen {0}", tmpUser);
                waClient.SendQueryLastOnline(tmpUser.GetFullJid());
                Thread.Sleep(1000);

                // load profile photo
                OnGetPictureDelegate onGetPhotoHandler = (from, id, data) =>
                {
                    Console.WriteLine("PHOTO: from={0}, id={1}", from, id);
                    File.WriteAllBytes(Path.Combine(photosDir, phone + ".png"), data);
                    result.PhotoFilename = Path.Combine(photosDir, phone + ".png");
                };
                waClient.OnGetPhoto += onGetPhotoHandler;
                Console.WriteLine("[] Request profile photo for {0}", tmpUser);
                waClient.SendGetPhoto(tmpUser.GetFullJid(), null, true);
                Thread.Sleep(1000);

                // get status
                OnGetStatusDelegate onGetStatusHandler = (from, type, name, status) =>
                {
                    Console.WriteLine("Status: from={0}, type={1}, name={2}, status={3}", from, type, name, status);
                    result.Status = status;
                };
                waClient.OnGetStatus += onGetStatusHandler;
                Console.WriteLine("[] Request profile status for {0}", tmpUser);
                waClient.SendGetStatuses(new string[] { tmpUser.GetFullJid() });
                Thread.Sleep(1000);

                // unsubscribe
                waClient.OnGetLastSeen -= onGetLastSeenHandler;
                waClient.OnGetPhoto    -= onGetPhotoHandler;
                waClient.OnGetStatus   -= onGetStatusHandler;

                result.IsRegistered = (result.LastSeen != null) || (result.PhotoFilename != null) || (result.Status != null);
            }

            return(result);
        }