コード例 #1
0
 public Task FetchBubbleGroupAddress(Tuple <Contact, Contact.ID>[] contacts, Action <bool, string> result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         if (contacts.Length > 1)
         {
             var userContacts = await FetchContacts();
             var inputUsers = new List <IInputUser>();
             var names = new List <string>();
             foreach (var contact in contacts)
             {
                 names.Add(contact.Item1.FullName);
                 var id = uint.Parse(contact.Item2.Id);
                 foreach (var userContact in userContacts)
                 {
                     if (userContact.Id == id)
                     {
                         var inputUser = TelegramUtils.CastUserToInputUser(userContact);
                         if (inputUser != null)
                         {
                             inputUsers.Add(inputUser);
                             break;
                         }
                     }
                 }
             }
             if (inputUsers.Any())
             {
                 var subject = BubbleGroupUtils.GeneratePartyTitle(names.ToArray());
                 if (subject.Length > 25)
                 {
                     subject = subject.Substring(0, 22);
                     subject += "...";
                 }
                 using (var client = new FullClientDisposable(this))
                 {
                     var response = await client.Client.Methods.MessagesCreateChatAsync(
                         new MessagesCreateChatArgs
                     {
                         Users = inputUsers,
                         Title = subject,
                     });
                     ProcessIncomingPayload(response, true);
                     SaveState(response);
                     var chat = TelegramUtils.GetChatFromStatedMessage(response);
                     result(true, TelegramUtils.GetChatId(chat));
                 }
             }
             else
             {
                 result(false, null);
             }
         }
         else
         {
             result(true, contacts[0].Item2.Id);
         }
     }));
 }
コード例 #2
0
 public Task GetContactsFavorites(Action <List <Contact> > result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         var count = 0;
         var inputUsers = new List <IInputUser>();
         foreach (var bubbleGroup in BubbleGroupManager.SortByMostPopular(this, true))
         {
             var address = bubbleGroup.Address;
             foreach (var user in _dialogs.Users)
             {
                 var userId = TelegramUtils.GetUserId(user);
                 if (userId == address)
                 {
                     var inputUser = TelegramUtils.CastUserToInputUser(user);
                     if (inputUser != null)
                     {
                         inputUsers.Add(inputUser);
                         break;
                     }
                 }
             }
             count++;
             if (count > 6)
             {
                 break;
             }
         }
         if (!inputUsers.Any())
         {
             result(null);
         }
         else
         {
             using (var client = new FullClientDisposable(this))
             {
                 var users = await GetUsers(inputUsers, client.Client);
                 var contacts = users.Select(x =>
                                             new TelegramContact
                 {
                     Available = TelegramUtils.GetAvailable(x),
                     LastSeen = TelegramUtils.GetLastSeenTime(x),
                     FirstName = TelegramUtils.GetUserName(x),
                     Ids = new List <Contact.ID>
                     {
                         new Contact.ID
                         {
                             Service = this,
                             Id = TelegramUtils.GetUserId(x).ToString(CultureInfo.InvariantCulture)
                         }
                     },
                 }).OfType <Contact>().OrderBy(x => x.FirstName).ToList();
                 result(contacts);
             }
         }
     }));
 }
コード例 #3
0
        public Task GetUserInformation(string address, Action <Disa.Framework.UserInformation> result)
        {
            return(Task.Factory.StartNew(async() =>
            {
                foreach (var user in _dialogs.Users)
                {
                    var userId = TelegramUtils.GetUserId(user);
                    if (userId == address)
                    {
                        using (var client = new FullClientDisposable(this))
                        {
                            var inputUser = TelegramUtils.CastUserToInputUser(user);
                            var updatedUser = await GetUser(inputUser, client.Client);
                            var name = TelegramUtils.GetUserName(updatedUser);
                            var lastSeen = TelegramUtils.GetLastSeenTime(updatedUser);
                            var presence = TelegramUtils.GetAvailable(updatedUser);
                            var phoneNumber = TelegramUtils.GetUserPhoneNumber(updatedUser);

                            if (string.IsNullOrWhiteSpace(name))
                            {
                                result(null);  //TODO: ensure this doesn't crash Disa
                                return;
                            }
                            result(new UserInformation
                            {
                                Title = name,
                                SubtitleType = string.IsNullOrWhiteSpace(phoneNumber) ?
                                               UserInformation.TypeSubtitle.Other : UserInformation.TypeSubtitle.PhoneNumber,
                                Subtitle = string.IsNullOrWhiteSpace(phoneNumber) ?
                                           address : TelegramUtils.ConvertTelegramPhoneNumberIntoInternational(phoneNumber),
                                LastSeen = lastSeen,
                                Presence = presence,
                            });
                            return;
                        }
                    }
                }
                result(null);  //TODO: ensure this doesn't crash Disa
            }));
        }
コード例 #4
0
 public Task FetchBubbleGroupAddress(Tuple <Contact, Contact.ID>[] contacts, Action <bool, string> result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         if (contacts.Length > 1)
         {
             var inputUsers = new List <IInputUser>();
             var names = new List <string>();
             foreach (var contact in contacts)
             {
                 names.Add(contact.Item1.FullName);
                 var telegramContact = contact.Item1 as TelegramContact;
                 var inputUser = TelegramUtils.CastUserToInputUser(telegramContact.User);
                 if (inputUser != null)
                 {
                     inputUsers.Add(inputUser);
                 }
             }
             if (inputUsers.Any())
             {
                 var subject = BubbleGroupUtils.GeneratePartyTitle(names.ToArray());
                 if (subject.Length > 25)
                 {
                     subject = subject.Substring(0, 22);
                     subject += "...";
                 }
                 using (var client = new FullClientDisposable(this))
                 {
                     try
                     {
                         var response = await client.Client.Methods.MessagesCreateChatAsync(
                             new MessagesCreateChatArgs
                         {
                             Users = inputUsers,
                             Title = subject,
                         });
                         var updates = response as Updates;
                         if (updates != null)
                         {
                             SendToResponseDispatcher(updates, client.Client);
                             _dialogs.AddUsers(updates.Users);
                             _dialogs.AddChats(updates.Chats);
                             var chat = TelegramUtils.GetChatFromUpdate(updates);
                             result(true, TelegramUtils.GetChatId(chat));
                         }
                         else
                         {
                             result(false, null);
                         }
                     }
                     catch (Exception e)
                     {
                         //we get an exception if the user is not allowed to create groups
                         var rpcError = e as RpcErrorException;
                         if (rpcError != null)
                         {
                             result(false, null);
                         }
                     }
                 }
             }
             else
             {
                 result(false, null);
             }
         }
         else
         {
             if (contacts[0].Item2 is Contact.PartyID)
             {
                 JoinChannelIfLeft(contacts[0].Item2.Id);
                 result(true, contacts[0].Item2.Id);
             }
             else
             {
                 var telegramContact = contacts[0].Item1 as TelegramContact;
                 _dialogs.AddUser(telegramContact.User);
                 result(true, contacts[0].Item2.Id);
             }
         }
     }));
 }
コード例 #5
0
        public Task SendBotInlineModeQuery(BubbleGroup group, Bots.UpdateBotInlineQuery query, Action <Bots.MessagesBotResults> results)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var client = new FullClientDisposable(this))
                {
                    var telegramBotContact = query.Bot as TelegramBotContact;
                    var user = telegramBotContact.User;

                    var args = new MessagesGetInlineBotResultsArgs
                    {
                        Bot = TelegramUtils.CastUserToInputUser(user),
                        Peer = GetInputPeer(group.Address, group.IsParty, group.IsExtendedParty),
                        Query = query.Query,
                        Offset = query.Offset
                    };

                    SharpTelegram.Schema.MessagesBotResults telegramMessageBotResults =
                        (SharpTelegram.Schema.MessagesBotResults)
                        TelegramUtils.RunSynchronously(
                            client.Client.Methods.MessagesGetInlineBotResultsAsync(args));

                    var messageBotResults = new Bots.MessagesBotResults
                    {
                        Gallery = telegramMessageBotResults.Gallery != null ? true : false,
                        QueryId = telegramMessageBotResults.QueryId.ToString(),
                        NextOffset = telegramMessageBotResults.NextOffset,
                        Results = new List <Bots.BotInlineResultBase>()
                    };

                    if (telegramMessageBotResults.Results != null)
                    {
                        foreach (var telegramMessageBotResult in telegramMessageBotResults.Results)
                        {
                            Bots.BotInlineResultBase item = null;
                            if (telegramMessageBotResult is SharpTelegram.Schema.BotInlineResult)
                            {
                                var telegramItem = telegramMessageBotResult as SharpTelegram.Schema.BotInlineResult;
                                item = new Bots.BotInlineResult
                                {
                                    Id = telegramItem.Id,
                                    QueryId = telegramMessageBotResults.QueryId.ToString(),
                                    Type = telegramItem.Type,
                                    Title = telegramItem.Title,
                                    Description = telegramItem.Description,
                                    Url = telegramItem.Url,
                                    ThumbUrl = telegramItem.ThumbUrl,
                                    ContentUrl = telegramItem.ContentUrl,
                                    ContentType = telegramItem.ContentType,
                                    W = telegramItem.W,
                                    H = telegramItem.H,
                                    Duration = telegramItem.Duration,
                                    SendMessage = HandleInlineModeSendMessage(telegramItem.SendMessage, group)
                                };
                            }
                            else if (telegramMessageBotResult is SharpTelegram.Schema.BotInlineMediaResult)
                            {
                                var telegramItem = telegramMessageBotResult as SharpTelegram.Schema.BotInlineMediaResult;
                                item = new Bots.BotInlineMediaResult
                                {
                                    Id = telegramItem.Id,
                                    QueryId = telegramMessageBotResults.QueryId.ToString(),
                                    Type = telegramItem.Type,
                                    Title = telegramItem.Title,
                                    Description = telegramItem.Description,
                                    SendMessage = HandleInlineModeSendMessage(telegramItem.SendMessage, group)
                                };

                                var botInlineMediaResult = item as Bots.BotInlineMediaResult;
                                botInlineMediaResult.Photo = HandleInlineModePhoto(telegramItem.Photo);

                                botInlineMediaResult.Document = HandleInlineModeDocument(telegramItem.Document);
                            }
                            else
                            {
                                continue;
                            }

                            messageBotResults.Results.Add(item);
                        }
                    }

                    results(messageBotResults);
                }
            }));
        }
コード例 #6
0
        private void GetSoloMentions(BubbleGroup group, Action <List <Mention> > result)
        {
            using (var client = new FullClientDisposable(this))
            {
                var resultList = new List <Mention>();

                var user      = _dialogs.GetUser(uint.Parse(group.Address));
                var inputUser = TelegramUtils.CastUserToInputUser(user);

                UserFull userFull =
                    (UserFull)
                    TelegramUtils.RunSynchronously(
                        client.Client.Methods.UsersGetFullUserAsync(new UsersGetFullUserArgs
                {
                    Id = inputUser
                }));

                if (userFull == null)
                {
                    result(resultList);
                }

                var telegramBotInfo = userFull.BotInfo as SharpTelegram.Schema.BotInfo;
                if (telegramBotInfo != null)
                {
                    var username = TelegramUtils.GetUserHandle(user);
                    var name     = TelegramUtils.GetUserName(user);

                    var botCommandMention = new Mention
                    {
                        Type          = MentionType.BotCommand,
                        BubbleGroupId = group.ID,
                        Value         = username,
                        Name          = name,
                        Address       = telegramBotInfo.UserId.ToString(CultureInfo.InvariantCulture)
                    };

                    var disaBotInfo = new Disa.Framework.Bots.BotInfo
                    {
                        Address     = telegramBotInfo.UserId.ToString(CultureInfo.InvariantCulture),
                        Description = telegramBotInfo.Description,
                        Commands    = new List <Disa.Framework.Bots.BotCommand>()
                    };

                    foreach (var c in telegramBotInfo.Commands)
                    {
                        var telegramBotCommand = c as SharpTelegram.Schema.BotCommand;
                        if (telegramBotCommand != null)
                        {
                            disaBotInfo.Commands.Add(new Disa.Framework.Bots.BotCommand
                            {
                                Command     = telegramBotCommand.Command,
                                Description = telegramBotCommand.Description
                            });
                        }
                    }

                    botCommandMention.BotInfo = disaBotInfo;

                    resultList.Add(botCommandMention);
                }

                result(resultList);
            }
        }
コード例 #7
0
        public Task InviteToChannel(BubbleGroup group, Tuple <Contact, Contact.ID>[] contacts, Action <bool> result)
        {
            var addPartyResult = AddPartyResult.Success;

            return(Task.Factory.StartNew(() =>
            {
                if (contacts.Length > 0)
                {
                    var inputUsers = new List <IInputUser>();
                    var names = new List <string>();
                    foreach (var contact in contacts)
                    {
                        names.Add(contact.Item1.FullName);
                        IUser user = null;
                        if (contact.Item1 is TelegramContact)
                        {
                            user = (contact.Item1 as TelegramContact).User;
                        }
                        else if (contact.Item1 is TelegramBotContact)
                        {
                            user = (contact.Item1 as TelegramBotContact).User;
                        }
                        var inputUser = TelegramUtils.CastUserToInputUser(user);
                        if (inputUser != null)
                        {
                            inputUsers.Add(inputUser);
                        }
                    }

                    if (inputUsers.Any())
                    {
                        using (var client = new FullClientDisposable(this))
                        {
                            try
                            {
                                var update = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsInviteToChannelAsync(new ChannelsInviteToChannelArgs
                                {
                                    Channel = new InputChannel
                                    {
                                        ChannelId = uint.Parse(group.Address),
                                        AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                                    },
                                    Users = inputUsers
                                }));
                                SendToResponseDispatcher(update, client.Client);
                            }
                            catch (Exception ex)
                            {
                                Utils.DebugPrint("Failed Telegram ChannelsInviteToChannelAsync: " + ex);

                                if (ex.Message.Contains("PEER_FLOOD"))
                                {
                                    _addPartyResult = AddPartyResult.Flood;
                                }
                                else
                                {
                                    _addPartyResult = AddPartyResult.Error;
                                }
                                return;
                            }
                        }
                    }
                }
            }));
        }