Example #1
0
 public static TelegramSettings GenerateAuthentication(Service service)
 {
     try
     {
         service.DebugPrint("Fetching nearest DC...");
         var settings = new TelegramSettings();
         var authInfo = TelegramUtils.RunSynchronously(FetchNewAuthentication(DefaultTransportConfig));
         using (var client = new TelegramClient(DefaultTransportConfig,
                                                new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo))
         {
             TelegramUtils.RunSynchronously(client.Connect());
             var nearestDcId = (NearestDc)TelegramUtils.RunSynchronously(client.Methods.HelpGetNearestDcAsync(new HelpGetNearestDcArgs {
             }));
             var config      = (Config)TelegramUtils.RunSynchronously(client.Methods.HelpGetConfigAsync(new HelpGetConfigArgs {
             }));
             var dcOption    = config.DcOptions.OfType <DcOption>().FirstOrDefault(x => x.Id == nearestDcId.NearestDcProperty);
             settings.NearestDcId   = nearestDcId.NearestDcProperty;
             settings.NearestDcIp   = dcOption.IpAddress;
             settings.NearestDcPort = (int)dcOption.Port;
         }
         service.DebugPrint("Generating authentication on nearest DC...");
         var authInfo2 = TelegramUtils.RunSynchronously(FetchNewAuthentication(
                                                            new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort)));
         settings.AuthKey = authInfo2.AuthKey;
         settings.Salt    = authInfo2.Salt;
         service.DebugPrint("Great! Ready for the service to start.");
         return(settings);
     }
     catch (Exception ex)
     {
         service.DebugPrint("Error in GenerateAuthentication: " + ex);
     }
     return(null);
 }
Example #2
0
 public Task SetPartyDescription(BubbleGroup group, string description, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         if (description != null)
         {
             using (var client = new FullClientDisposable(this))
             {
                 var edited = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsEditAboutAsync(new ChannelsEditAboutArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     About = description
                 }));
                 if (edited)
                 {
                     result(true);
                 }
                 else
                 {
                     result(false);
                 }
             }
         }
     }));
 }
Example #3
0
 public static AuthPasswordRecovery RequestPasswordRecovery(Service service, TelegramSettings settings)
 {
     try
     {
         var transportConfig =
             new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort);
         using (var client = new TelegramClient(transportConfig,
                                                new ConnectionConfig(settings.AuthKey, settings.Salt), AppInfo))
         {
             TelegramUtils.RunSynchronously(client.Connect());
             try
             {
                 var result = (AuthPasswordRecovery)TelegramUtils.RunSynchronously(client.Methods.AuthRequestPasswordRecoveryAsync(new AuthRequestPasswordRecoveryArgs
                 {
                 }));
                 Thread.Sleep(100);
                 return(result);
             }
             catch (RpcErrorException ex)
             {
                 var error = (RpcError)ex.Error;
                 return(null);
             }
         }
     }
     catch (Exception ex)
     {
         service.DebugPrint("Error in VerifyPassword: " + ex);
         return(null);
     }
 }
Example #4
0
 private bool CheckLink(string linkUsefulPart, out IChat alreadyJoinedChat)
 {
     using (var client = new FullClientDisposable(this))
     {
         try
         {
             var result = TelegramUtils.RunSynchronously(client.Client.Methods.MessagesCheckChatInviteAsync(new MessagesCheckChatInviteArgs
             {
                 Hash = linkUsefulPart
             }));
             var chatInvite        = result as ChatInvite;
             var chatInviteAlready = result as ChatInviteAlready;
             if (chatInvite != null)
             {
                 alreadyJoinedChat = null;
                 return(true);
             }
             if (chatInviteAlready != null)
             {
                 alreadyJoinedChat = chatInviteAlready.Chat;
                 return(true);
             }
             alreadyJoinedChat = null;
             return(false);
         }
         catch (Exception ex)
         {
             DebugPrint("Exception while checking invite" + ex);
             alreadyJoinedChat = null;
             return(false);
         }
     }
 }
Example #5
0
        private uint GetLastPtsForChannel(TelegramClient client, string address)
        {
            uint offset = 0;

            uint pts = 0;

Again:

            var iDialogs = TelegramUtils.RunSynchronously(client.Methods.MessagesGetDialogsAsync(new MessagesGetDialogsArgs
            {
                Limit      = 100,
                OffsetPeer = new InputPeerEmpty()
            }));
            var dialogs      = iDialogs as MessagesDialogs;
            var dialogsSlice = iDialogs as MessagesDialogsSlice;

            if (dialogs != null)
            {
                pts = FindPtsFromDialogs(dialogs.Dialogs, address);
            }
            if (dialogsSlice != null)
            {
                pts = FindPtsFromDialogs(dialogsSlice.Dialogs, address);
                //pts can never be zero, so we use this
                if (pts == 0)
                {
                    offset += dialogsSlice.Count;
                    goto Again;
                }
            }

            return(pts);
        }
Example #6
0
 public static bool RequestAccountReset(Service service, TelegramSettings settings)
 {
     try
     {
         var transportConfig =
             new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort);
         using (var client = new TelegramClient(transportConfig,
                                                new ConnectionConfig(settings.AuthKey, settings.Salt), AppInfo))
         {
             TelegramUtils.RunSynchronously(client.Connect());
             try
             {
                 var result = TelegramUtils.RunSynchronously(client.Methods.AccountDeleteAccountAsync(new AccountDeleteAccountArgs
                 {
                     Reason = "Forgot Password"
                 }));
                 return(result);
             }
             catch (RpcErrorException ex)
             {
                 var error = (RpcError)ex.Error;
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         service.DebugPrint("Error in VerifyPassword: " + ex);
         return(false);
     }
 }
Example #7
0
        private List <IChannelParticipant> GetChannelParticipants(ChannelFull channelFull, IChannelParticipantsFilter filter)
        {
            var participantsList = new List <IChannelParticipant>();

            using (var client = new FullClientDisposable(this))
            {
                uint count  = 100;
                uint offset = 0;
                var  result = (ChannelsChannelParticipants)TelegramUtils.RunSynchronously(
                    client.Client.Methods.ChannelsGetParticipantsAsync(new ChannelsGetParticipantsArgs
                {
                    Channel = new InputChannel
                    {
                        ChannelId  = channelFull.Id,
                        AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(channelFull.Id))
                    },
                    Filter = filter,
                    Limit  = 100,
                    Offset = offset
                }));
                participantsList.AddRange(result.Participants);
                _dialogs.AddUsers(result.Users);
            }
            return(participantsList);
        }
Example #8
0
 public Task SetPartyName(BubbleGroup group, string name)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             if (!group.IsExtendedParty)
             {
                 var update = TelegramUtils.RunSynchronously(
                     client.Client.Methods.MessagesEditChatTitleAsync(new MessagesEditChatTitleArgs
                 {
                     ChatId = uint.Parse(group.Address),
                     Title = name,
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
             else
             {
                 var update = TelegramUtils.RunSynchronously(
                     client.Client.Methods.ChannelsEditTitleAsync(new ChannelsEditTitleArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Title = name
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
         }
     }));
 }
Example #9
0
 public Task SetPartyAddNewMembersRestriction(BubbleGroup group, PartyOptionsSettingsAddNewMembersRestriction restriction, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             try
             {
                 var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsToggleInvitesAsync(new ChannelsToggleInvitesArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Enabled = restriction == PartyOptionsSettingsAddNewMembersRestriction.Everyone
                 }));
                 SendToResponseDispatcher(response, client.Client);
                 result(true);
             }
             catch (Exception e)
             {
                 DebugPrint("##### Exeption while setting all members are admins type " + e);
                 result(false);
             }
         }
     }));
 }
Example #10
0
        public Task GetBotCallbackAnswer(BubbleGroup group, VisualBubble bubble, Bots.KeyboardButton button, Action <Bots.MessagesBotCallbackAnswer> answer)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var client = new FullClientDisposable(this))
                {
                    // Note: Telegram also has KeyboardButtonGame which functions as a callback also
                    if (button is Bots.KeyboardButtonCallback)
                    {
                        var keyboardButtonCallback = button as Bots.KeyboardButtonCallback;

                        var args = new MessagesGetBotCallbackAnswerArgs
                        {
                            MsgId = uint.Parse(bubble.IdService),
                            Data = keyboardButtonCallback.Data,
                            Peer = GetInputPeer(group.Address, group.IsParty, group.IsExtendedParty)
                        };

                        SharpTelegram.Schema.MessagesBotCallbackAnswer telegramBotCallbackAnswer =
                            (SharpTelegram.Schema.MessagesBotCallbackAnswer)
                            TelegramUtils.RunSynchronously(
                                client.Client.Methods.MessagesGetBotCallbackAnswerAsync(args));

                        var disaBotCallbackAnswer = new Bots.MessagesBotCallbackAnswer
                        {
                            Alert = telegramBotCallbackAnswer.Alert != null ? true : false,
                            Message = telegramBotCallbackAnswer.Message
                        };

                        answer(disaBotCallbackAnswer);
                    }
                }
            }));
        }
Example #11
0
        private IMessagesMessages GetChatMessagesForChannel(BubbleGroup group, long fromTime, int max, FullClientDisposable client)
        {
            var fullChat    = FetchFullChat(group.Address, true);
            var fullChannel = fullChat.FullChat as ChannelFull;

            if (fullChannel != null)
            {
                if (fullChannel.MigratedFromChatId != 0)
                {
                    var peerChat = GetInputPeer(fullChannel.MigratedFromChatId.ToString(), true, false);
                    DisposeFullChat();
                    return(TelegramUtils.RunSynchronously(
                               client.Client.Methods.MessagesGetHistoryAsync(new MessagesGetHistoryArgs
                    {
                        Peer = peerChat,
                        OffsetDate = (uint)fromTime,
                        Limit = (uint)max
                    })));
                }
                DisposeFullChat();
                return(MakeMessagesMessages(new List <IChat>(), new List <IUser>(), new List <IMessage>()));
            }
            DisposeFullChat();
            return(MakeMessagesMessages(new List <IChat>(), new List <IUser>(), new List <IMessage>()));
        }
 public Task SignMessages(BubbleGroup group, bool sign, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             try
             {
                 var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsToggleSignaturesAsync(new ChannelsToggleSignaturesArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Enabled = sign
                 }));
                 SendToResponseDispatcher(response, client.Client);
                 result(true);
             }
             catch (Exception e)
             {
                 DebugPrint("## exception while migrating the chat " + e);
                 result(false);
             }
         }
     }));
 }
Example #13
0
 public Task UnblockPartyParticipant(BubbleGroup group, DisaParticipant participant, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         using (var client = new FullClientDisposable(this))
         {
             try
             {
                 var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsKickFromChannelAsync(new ChannelsKickFromChannelArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     },
                     Kicked = false,
                     UserId = new InputUser
                     {
                         UserId = uint.Parse(participant.Address),
                         AccessHash = GetUserAccessHashIfForeign(participant.Address)
                     }
                 }));
                 result(true);
             }
             catch (Exception e)
             {
                 DebugPrint("#### Exception " + e);
                 result(false);
             }
         }
     }));
 }
Example #14
0
 public Task LeaveParty(BubbleGroup group)
 {
     return(Task.Factory.StartNew(() =>
     {
         var inputUser = new InputUser {
             UserId = Settings.AccountId
         };
         using (var client = new FullClientDisposable(this))
         {
             if (!group.IsExtendedParty)
             {
                 var update = TelegramUtils.RunSynchronously(client.Client.Methods.MessagesDeleteChatUserAsync(new MessagesDeleteChatUserArgs
                 {
                     ChatId = uint.Parse(group.Address),
                     UserId = inputUser,
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
             else
             {
                 var update = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsLeaveChannelAsync(new ChannelsLeaveChannelArgs
                 {
                     Channel = new InputChannel
                     {
                         ChannelId = uint.Parse(group.Address),
                         AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                     }
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
         }
     }));
 }
Example #15
0
        public Task DemotePartyParticipantsFromLeader(BubbleGroup group, DisaParticipant participant, Action <DemotePartyParticipantsResult> result)
        {
            return(Task.Factory.StartNew(() =>
            {
                var inputUser = new InputUser
                {
                    UserId = uint.Parse(participant.Address),
                    AccessHash = GetUserAccessHashIfForeign(participant.Address)
                };

                using (var client = new FullClientDisposable(this))
                {
                    if (!group.IsExtendedParty)
                    {
                        if (!ChatAdminsEnabled(group.Address))
                        {
                            result(DemotePartyParticipantsResult.AllMembersAreAdministratorsEnabled);
                            return;
                        }
                        try
                        {
                            TelegramUtils.RunSynchronously(client.Client.Methods.MessagesEditChatAdminAsync(new MessagesEditChatAdminArgs
                            {
                                ChatId = uint.Parse(group.Address),
                                IsAdmin = false,
                                UserId = inputUser,
                            }));
                            result(DemotePartyParticipantsResult.Success);
                        }
                        catch (Exception e)
                        {
                            result(DemotePartyParticipantsResult.Failure);
                        }
                    }
                    else
                    {
                        try
                        {
                            var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsEditAdminAsync(new ChannelsEditAdminArgs
                            {
                                Channel = new InputChannel
                                {
                                    ChannelId = uint.Parse(group.Address),
                                    AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                                },
                                Role = new ChannelRoleEmpty(),
                                UserId = inputUser
                            }));
                            SendToResponseDispatcher(response, client.Client);
                            result(DemotePartyParticipantsResult.Success);
                        }
                        catch (Exception e)
                        {
                            result(DemotePartyParticipantsResult.Failure);
                        }
                    }
                }
            }));
        }
Example #16
0
        public Task GetPrivacyList(Action <string[]> addresses)
        {
            List <string> addressList;

            return(Task.Factory.StartNew(() =>
            {
                uint offset = 0;
                //TODO: check when it returns a chunk or a full list
                using (var client = new FullClientDisposable(this))
                {
                    Again:
                    var blocked =
                        TelegramUtils.RunSynchronously(
                            client.Client.Methods.ContactsGetBlockedAsync(new ContactsGetBlockedArgs
                    {
                        Limit = 100,
                        Offset = offset
                    }));
                    var contactsBlocked = blocked as ContactsBlocked;
                    var contactsBlockedSlice = blocked as ContactsBlockedSlice;

                    addressList = new List <string>();
                    if (contactsBlocked != null)
                    {
                        foreach (var blockedContact in contactsBlocked.Blocked)
                        {
                            var contactBlocked = blockedContact as ContactBlocked;
                            if (contactBlocked == null)
                            {
                                continue;
                            }
                            addressList.Add(contactBlocked.UserId.ToString(CultureInfo.InvariantCulture));
                        }
                        _dialogs.AddUsers(contactsBlocked.Users);
                    }
                    if (contactsBlockedSlice != null)
                    {
                        foreach (var blockedContact in contactsBlockedSlice.Blocked)
                        {
                            var contactBlocked = blockedContact as ContactBlocked;
                            if (contactBlocked == null)
                            {
                                continue;
                            }
                            addressList.Add(contactBlocked.UserId.ToString(CultureInfo.InvariantCulture));
                        }
                        _dialogs.AddUsers(contactsBlockedSlice.Users);
                        if (contactsBlockedSlice.Blocked.Any())
                        {
                            offset += contactsBlockedSlice.Count;
                            goto Again;
                        }
                    }
                    previousAddresses = addressList.ToArray();
                    addresses(addressList.ToArray());
                }
            }));
        }
Example #17
0
            public DownloadManager(Document document, Telegram telegram)
            {
                _document = document;
                _fileSize = document.Size;
                _telegram = telegram;
                int dcId = (int)_document.DcId;

                //initialize the dc we want to download from
                if (_document.DcId != _telegram.Settings.NearestDcId)
                {
Retry:
                    // if its not the nearest dc, it could be unintialized. so lets check and init it.
                    var dc = DcDatabase.Get(dcId);
                    if (dc == null)
                    {
                        _telegram.GetClient(dcId);
                        //after this our dc configuration is ready, the connection should die out in a 60 secs.
                    }
                    var dcCached = DcDatabase.Get(dcId);
                    if (dcCached == null)
                    {
                        return;
                    }
                    var dcOption  = _telegram.Config.DcOptions.Cast <DcOption>().FirstOrDefault(x => x.Id == dcId);
                    var tcpConfig = new TcpClientTransportConfig(dcOption.IpAddress, (int)dcOption.Port);
                    var authInfo  = new SharpMTProto.Authentication.AuthInfo(dcCached.Key,
                                                                             BitConverter.ToUInt64(dcCached.Salt, 0));
                    _client = new TelegramClient(tcpConfig,
                                                 new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo);
                    TelegramUtils.RunSynchronously(_client.Connect());
                    try
                    {
                        TelegramUtils.RunSynchronously(_client.Methods.AccountGetAccountTTLAsync(new AccountGetAccountTTLArgs()));
                    }
                    catch (Exception x)
                    {
                        if (x.Message != null && x.Message.Contains("401"))
                        {
                            telegram.DebugPrint("Yikes! DC went down. Let's reinstate it...");
                            _client.Dispose();
                            DcDatabase.Delete(dcId);
                            goto Retry;
                        }
                    }
                }
                else
                {
                    var tcpConfig = new TcpClientTransportConfig(_telegram.Settings.NearestDcIp, (int)_telegram.Settings.NearestDcPort);
                    var authInfo  = new SharpMTProto.Authentication.AuthInfo(_telegram.Settings.AuthKey,
                                                                             _telegram.Settings.Salt);
                    _client = new TelegramClient(tcpConfig,
                                                 new ConnectionConfig(authInfo.AuthKey, authInfo.Salt), AppInfo);
                    TelegramUtils.RunSynchronously(_client.Connect());
                }
            }
Example #18
0
        public Task SetPrivacyList(string[] addresses)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var client = new FullClientDisposable(this))
                {
                    if (addresses.Length < previousAddresses.Length)
                    {
                        //address was removed
                        foreach (var address in previousAddresses)
                        {
                            if (addresses.Contains(address))
                            {
                                continue;
                            }

                            TelegramUtils.RunSynchronously(
                                client.Client.Methods.ContactsUnblockAsync(new ContactsUnblockArgs
                            {
                                Id = new InputUser
                                {
                                    UserId = uint.Parse(address),
                                    AccessHash = GetUserAccessHashIfForeign(address)
                                }
                            }));
                            break;
                        }
                    }
                    else //address was added
                    {
                        foreach (var address in addresses)
                        {
                            if (previousAddresses.Contains(address))
                            {
                                continue;
                            }

                            TelegramUtils.RunSynchronously(
                                client.Client.Methods.ContactsBlockAsync(new ContactsBlockArgs
                            {
                                Id = new InputUser
                                {
                                    UserId = uint.Parse(address),
                                    AccessHash = GetUserAccessHashIfForeign(address)
                                }
                            }));
                            break;
                        }
                    }
                }
                ServiceEvents.RaisePrivacyListUpdated();
                previousAddresses = addresses;
            }));
        }
Example #19
0
 private MessagesChatFull FetchFullChat(string address, bool superGroup)
 {
     //Classic check lock check pattern for concurrent access from all the methods
     if (_fullChat != null)
     {
         return(_fullChat);
     }
     lock (_fullChatLock)
     {
         if (_fullChat != null)
         {
             return(_fullChat);
         }
         using (var client = new FullClientDisposable(this))
         {
             if (!superGroup)
             {
                 _fullChat =
                     (MessagesChatFull)
                     TelegramUtils.RunSynchronously(
                         client.Client.Methods.MessagesGetFullChatAsync(new MessagesGetFullChatArgs
                 {
                     ChatId = uint.Parse(address)
                 }));
             }
             else
             {
                 try
                 {
                     _fullChat =
                         (MessagesChatFull)
                         TelegramUtils.RunSynchronously(
                             client.Client.Methods.ChannelsGetFullChannelAsync(new ChannelsGetFullChannelArgs
                     {
                         Channel = new InputChannel
                         {
                             ChannelId  = uint.Parse(address),
                             AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(address)))
                         }
                     }));
                 }
                 catch (Exception e)
                 {
                     DebugPrint(">>>> get full channel exception " + e);
                 }
             }
             //DebugPrint("#### fullchat " + ObjectDumper.Dump(_fullChat));
             _dialogs.AddUsers(_fullChat.Users);
             _dialogs.AddChats(_fullChat.Chats);
             return(_fullChat);
         }
     }
 }
Example #20
0
        private IMessagesMessages GetChannelMessages(BubbleGroup group, uint offsetId, int max, FullClientDisposable client)
        {
            var peer = GetInputPeer(group.Address, group.IsParty, group.IsExtendedParty);

            return
                (TelegramUtils.RunSynchronously(
                     client.Client.Methods.MessagesGetHistoryAsync(new MessagesGetHistoryArgs
            {
                Peer = peer,
                OffsetId = offsetId + 1,
                OffsetDate = 0,
                Limit = (uint)max
            })));
        }
Example #21
0
        public Task SetPartyPhoto(BubbleGroup group, byte[] bytes, Action <DisaThumbnail> result)
        {
            return(Task.Factory.StartNew(() =>
            {
                //MessagesEditChatPhotoAsync
                byte[] resizedImage = Platform.GenerateJpegBytes(bytes, 640, 640);
                var inputFile = UploadPartyImage(resizedImage);

                using (var client = new FullClientDisposable(this))
                {
                    if (!group.IsExtendedParty)
                    {
                        var update = TelegramUtils.RunSynchronously(
                            client.Client.Methods.MessagesEditChatPhotoAsync(new MessagesEditChatPhotoArgs
                        {
                            ChatId = uint.Parse(group.Address),
                            Photo = new InputChatUploadedPhoto
                            {
                                Crop = new InputPhotoCropAuto(),
                                File = inputFile
                            }
                        }));
                        SendToResponseDispatcher(update, client.Client);
                    }
                    else
                    {
                        //ChannelsEditPhoto
                        var update = TelegramUtils.RunSynchronously(
                            client.Client.Methods.ChannelsEditPhotoAsync(new ChannelsEditPhotoArgs
                        {
                            Channel = new InputChannel
                            {
                                ChannelId = uint.Parse(group.Address),
                                AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                            },
                            Photo = new InputChatUploadedPhoto
                            {
                                Crop = new InputPhotoCropAuto(),
                                File = inputFile
                            }
                        }));
                        SendToResponseDispatcher(update, client.Client);
                    }
                }
                byte[] disaImage = Platform.GenerateJpegBytes(bytes, 96, 96);
                var thumbnail = new DisaThumbnail(this, disaImage, GenerateRandomId().ToString());
                result(thumbnail);
            }));
        }
Example #22
0
 private IExportedChatInvite ExportChatInvite(BubbleGroup group)
 {
     using (var client = new FullClientDisposable(this))
     {
         var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsExportInviteAsync(new ChannelsExportInviteArgs
         {
             Channel = new InputChannel
             {
                 ChannelId  = uint.Parse(group.Address),
                 AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
             }
         }));
         return(response);
     }
 }
Example #23
0
        public static DcOption GetDcConfig(int migrateId, TelegramSettings settings)
        {
            var transportConfig =
                new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort);

            using (var client = new TelegramClient(transportConfig,
                                                   new ConnectionConfig(settings.AuthKey, settings.Salt), AppInfo))
            {
                TelegramUtils.RunSynchronously(client.Connect());
                var config   = (Config)TelegramUtils.RunSynchronously(client.Methods.HelpGetConfigAsync(new HelpGetConfigArgs {
                }));
                var dcOption = config.DcOptions.OfType <DcOption>().FirstOrDefault(x => x.Id == migrateId);
                return(dcOption);
            }
        }
Example #24
0
 public Task SetPartyType(BubbleGroup group, PartyOptionsSettingsPartyType type, Action <bool> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         try
         {
             if (type == PartyOptionsSettingsPartyType.Public)
             {
                 var randomUserName = RandomString(32);
                 DebugPrint("The random username is " + randomUserName);
                 using (var client = new FullClientDisposable(this))
                 {
                     var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsUpdateUsernameAsync(new SharpTelegram.Schema.ChannelsUpdateUsernameArgs
                     {
                         Channel = new InputChannel
                         {
                             ChannelId = uint.Parse(group.Address),
                             AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                         },
                         Username = randomUserName
                     }));
                     result(response);
                 }
             }
             else
             {
                 using (var client = new FullClientDisposable(this))
                 {
                     var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsUpdateUsernameAsync(new SharpTelegram.Schema.ChannelsUpdateUsernameArgs
                     {
                         Channel = new InputChannel
                         {
                             ChannelId = uint.Parse(group.Address),
                             AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                         },
                         Username = ""
                     }));
                     result(response);
                 }
             }
         }
         catch (Exception e)
         {
             DebugPrint("##### Exeption while setting group type " + e);
             result(false);
         }
     }));
 }
Example #25
0
 private bool SetChannelUserName(BubbleGroup group, string name)
 {
     using (var client = new FullClientDisposable(this))
     {
         var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsUpdateUsernameAsync(new ChannelsUpdateUsernameArgs
         {
             Channel = new InputChannel
             {
                 ChannelId  = uint.Parse(group.Address),
                 AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
             },
             Username = name
         }));
         return(response);
     }
 }
Example #26
0
        private static void GetPasswordDetails(TelegramClient client, CodeRegister cr)
        {
            var result = TelegramUtils.RunSynchronously(client.Methods.AccountGetPasswordAsync(new AccountGetPasswordArgs
            {
            }));
            var authPassword   = result as AccountPassword;
            var authNoPassword = result as AccountNoPassword;

            if (authPassword != null)
            {
                cr.CurrentSalt = authPassword.CurrentSalt;
                cr.NewSalt     = authPassword.NewSalt;
                cr.HasRecovery = authPassword.HasRecovery;
                cr.Hint        = authPassword.Hint;
            }
        }
Example #27
0
        public static AuthExportedAuthorization GenerateExportedAuth(int migrateId, TelegramSettings settings)
        {
            var transportConfig =
                new TcpClientTransportConfig(settings.NearestDcIp, settings.NearestDcPort);

            using (var client = new TelegramClient(transportConfig,
                                                   new ConnectionConfig(settings.AuthKey, settings.Salt), AppInfo))
            {
                TelegramUtils.RunSynchronously(client.Connect());
                var exportedAuth = (AuthExportedAuthorization)TelegramUtils.RunSynchronously(client.Methods.AuthExportAuthorizationAsync(
                                                                                                 new SharpTelegram.Schema.AuthExportAuthorizationArgs
                {
                    DcId = (uint)migrateId,
                }));
                return(exportedAuth);
            }
        }
Example #28
0
        public string PhoneNumberToServiceAddress(string number)
        {
            var users           = TelegramUtils.RunSynchronously(FetchContacts());
            var formattedNumber = FormatNumber(number);

            if (formattedNumber == null)
            {
                return(null);
            }
            foreach (var user in users)
            {
                if (user.Phone == formattedNumber)
                {
                    return(user.Id.ToString());
                }
            }
            return(null);
        }
Example #29
0
 public Task DeletePartyParticipant(BubbleGroup group, DisaParticipant participant)
 {
     return(Task.Factory.StartNew(() =>
     {
         var inputUser = new InputUser
         {
             UserId = uint.Parse(participant.Address),
             AccessHash = GetUserAccessHashIfForeign(participant.Address)
         };
         using (var client = new FullClientDisposable(this))
         {
             if (!group.IsExtendedParty)
             {
                 var update = TelegramUtils.RunSynchronously(client.Client.Methods.MessagesDeleteChatUserAsync(new MessagesDeleteChatUserArgs
                 {
                     ChatId = uint.Parse(group.Address),
                     UserId = inputUser,
                 }));
                 SendToResponseDispatcher(update, client.Client);
             }
             else
             {
                 try
                 {
                     var update = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsKickFromChannelAsync(new ChannelsKickFromChannelArgs
                     {
                         Channel = new InputChannel
                         {
                             ChannelId = uint.Parse(group.Address),
                             AccessHash = TelegramUtils.GetChannelAccessHash(_dialogs.GetChat(uint.Parse(group.Address)))
                         },
                         Kicked = true,
                         UserId = inputUser
                     }));
                     SendToResponseDispatcher(update, client.Client);
                 }
                 catch (Exception e)
                 {
                     DebugPrint("Exception " + e);
                 }
             }
         }
     }));
 }
Example #30
0
 private void DisconnectFullClientIfPossible()
 {
     if (IsFullClientConnected)
     {
         try
         {
             TelegramUtils.RunSynchronously(_fullClient.Methods.AccountUpdateStatusAsync(new AccountUpdateStatusArgs
             {
                 Offline = true
             }));
             TelegramUtils.RunSynchronously(_fullClient.Disconnect());
         }
         catch (Exception ex)
         {
             DebugPrint("Failed to disconnect full client: " + ex);
         }
         RemoveFullClientPingIfPossible();
     }
 }