private async void SendContactExecute()
        {
            var picker = new ContactPicker();

            picker.SelectionMode = ContactSelectionMode.Fields;
            picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);

            var picked = await picker.PickContactAsync();

            if (picked != null)
            {
                Telegram.Td.Api.Contact contact = null;

                var annotationStore = await ContactManager.RequestAnnotationStoreAsync(ContactAnnotationStoreAccessType.AppAnnotationsReadWrite);

                var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);

                if (store != null && annotationStore != null)
                {
                    var full = await store.GetContactAsync(picked.Id);

                    if (full != null)
                    {
                        var annotations = await annotationStore.FindAnnotationsForContactAsync(full);

                        var first = annotations.FirstOrDefault();
                        if (first != null)
                        {
                            var remote = first.RemoteId;
                            if (int.TryParse(remote.Substring(1), out int userId))
                            {
                                var user = ProtoService.GetUser(userId);
                                if (user != null)
                                {
                                    contact = new Telegram.Td.Api.Contact(user.PhoneNumber, user.FirstName, user.LanguageCode, user.Id);
                                }
                            }
                        }

                        //contact = full;
                    }
                }

                if (contact == null)
                {
                    var phone = picked.Phones.FirstOrDefault();
                    if (phone == null)
                    {
                        return;
                    }

                    contact = new Telegram.Td.Api.Contact(phone.Number, picked.FirstName, picked.LastName, 0);
                }

                if (contact != null)
                {
                    await SendContactAsync(contact);
                }
            }
        }
 public Task <BaseObject> SendContactAsync(Telegram.Td.Api.Contact contact)
 {
     return(SendMessageAsync(0, new InputMessageContact(contact)));
 }
Exemplo n.º 3
0
        private async void SendContactExecute()
        {
            var picker = new ContactPicker();
            //picker.SelectionMode = ContactSelectionMode.Fields;
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Address);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.ConnectedServiceAccount);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.ImportantDate);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.JobInfo);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Notes);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.SignificantOther);
            //picker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Website);

            var picked = await picker.PickContactAsync();

            if (picked != null)
            {
                Telegram.Td.Api.Contact contact = null;
                string vcard = string.Empty;

                var annotationStore = await ContactManager.RequestAnnotationStoreAsync(ContactAnnotationStoreAccessType.AppAnnotationsReadWrite);

                var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite);

                if (store != null && annotationStore != null)
                {
                    var full = await store.GetContactAsync(picked.Id);

                    if (full != null)
                    {
                        var annotations = await annotationStore.FindAnnotationsForContactAsync(full);

                        //var vcardStream = await ContactManager.ConvertContactToVCardAsync(full, 2000);
                        //using (var stream = await vcardStream.OpenReadAsync())
                        //{
                        //    using (var dataReader = new DataReader(stream.GetInputStreamAt(0)))
                        //    {
                        //        await dataReader.LoadAsync((uint)stream.Size);
                        //        vcard = dataReader.ReadString(dataReader.UnconsumedBufferLength);
                        //    }
                        //}

                        var first = annotations.FirstOrDefault();
                        if (first != null)
                        {
                            var remote = first.RemoteId;
                            if (int.TryParse(remote.Substring(1), out int userId))
                            {
                                var user = ProtoService.GetUser(userId);
                                if (user != null)
                                {
                                    contact = new Telegram.Td.Api.Contact(user.PhoneNumber, user.FirstName, user.LastName, vcard, user.Id);
                                }
                            }
                        }

                        //contact = full;

                        if (contact == null)
                        {
                            var phone = full.Phones.FirstOrDefault();
                            if (phone == null)
                            {
                                return;
                            }

                            contact = new Telegram.Td.Api.Contact(phone.Number, picked.FirstName, picked.LastName, vcard, 0);
                        }
                    }
                }

                if (contact != null)
                {
                    await SendContactAsync(contact);
                }
            }
        }