Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

How offset getDialogs? #597

Closed
JhonDon opened this issue Oct 6, 2017 · 9 comments
Closed

How offset getDialogs? #597

JhonDon opened this issue Oct 6, 2017 · 9 comments

Comments

@JhonDon
Copy link

JhonDon commented Oct 6, 2017

public async Task<TLAbsDialogs> GetUserDialogsAsync()
{
    var peer = new TLInputPeerSelf();
    return await SendRequestAsync<TLAbsDialogs>(
        new TLRequestGetDialogs() { offset_peer = peer, limit = 10 });
}

TLRequestGetDialogs.cs:

*****
public int flags { get; set; }
public bool exclude_pinned { get; set; }
public int offset_date { get; set; }
public int offset_id { get; set; }
public TLAbsInputPeer offset_peer { get; set; }
public int limit { get; set; }
public Messages.TLAbsDialogs Response { get; set; }
*****

https://core.telegram.org/method/messages.getDialogs
Params:

offset int Number of list elements to be skipped
max_id int If a positive value was transmitted sent, the method will return only dialogs with IDs less than the set one
limit int Number of list elements to be returned

How in Sochix TLRequestGetDialogs Number of list elements to be skipped ?

@JhonDon
Copy link
Author

JhonDon commented Oct 6, 2017

messages.getDialogs#eccf1df6 offset:int max_id:int limit:int = messages.Dialogs;

Change cod TLRequestGetDialogs.cs:

public override void SerializeBody(BinaryWriter bw)
{
        /*bw.Write(Constructor);
        ComputeFlags();
        bw.Write(flags);

        bw.Write(offset_date);
        bw.Write(offset_id);
        ObjectUtils.SerializeObject(offset_peer, bw);
        bw.Write(limit);*/

        bw.Write(0xeccf1df6);
        bw.Write(5);   //  offset
        bw.Write(0);    //  max_id
        bw.Write(10);   //  limit
}

Limit working, but offset not working? help me!

@JhonDon
Copy link
Author

JhonDon commented Oct 6, 2017

Sochix Илья русский блин)
Это оказывается layer 66 такие правила использует, а телега 23 layer пишет. А у тебя случайно нету старых разработок с 23? устал переписывать и не работает особо

@NaderSetarei
Copy link

hi
let me know if you got any solutions ?
thanks

@hexiaoyuan
Copy link

bool _dialogsFull = false;
int _dialogsOffsetDate = 0;
int _dialogsOffsetId = 0;
TeleSharp.TL.TLAbsInputPeer _dialogsOffsetPeer = null;
var _inputPeerEmpty = new TeleSharp.TL.TLInputPeerEmpty();

while (!_dialogsFull){
	var response = await client.SendRequestAsync<TeleSharp.TL.Messages.TLAbsDialogs>(
		new TeleSharp.TL.Messages.TLRequestGetDialogs()
		{
			OffsetPeer = (_dialogsOffsetPeer != null ? _dialogsOffsetPeer : _inputPeerEmpty),
			OffsetId = _dialogsOffsetId,
			OffsetDate = _dialogsOffsetDate,
			Limit = _loadCount
		});
	if (response is TeleSharp.TL.Messages.TLDialogs){
		_dialogsFull = true; // // we didn't get a DialogsSlice which means we got all.
		var result = response as TeleSharp.TL.Messages.TLDialogs;
		foreach (var d in result.Dialogs) __handle_tg_dialog(d);
		foreach (var u in result.Users) __handle_tg_user(u);
		foreach (var c in result.Chats) __handle_tg_chat(c);
		foreach (var m in result.Messages) __handle_tg_message(m);
	}
	else if (response is TeleSharp.TL.Messages.TLDialogsSlice){
		var result = response as TeleSharp.TL.Messages.TLDialogsSlice;
		
		//Less than we requested means we reached the end.
		if (result.Dialogs.Count == _loadCount) _dialogsFull = false;
		
		int lastDate = 0;
		TeleSharp.TL.TLAbsPeer lastPeer = null;
		int lastMsgId = 0;

		for (int i = result.Dialogs.Count; i > 0;)
		{
			var dialog = result.Dialogs[--i];
			if (dialog.Peer != null) {
				if (0 == lastDate) {
					if (null == lastPeer) lastPeer = dialog.Peer;
					if (dialog.TopMessage > 0) {
						if (0 == lastMsgId) lastMsgId = dialog.TopMessage;
						for (int j = result.Messages.Count; j > 0;) {
							var msg = result.Messages[--j];
							if (dialog.TopMessage == __idFromTLAbsMessage(msg)) {
								var mpeer = __peerFromTLAbsMessage(msg);
								if (__isPeerEqual(mpeer, dialog.Peer)) {
									lastDate = __dateFromTLAbsMessage(msg);
									break;
								}
							}
						}
					}
				}
			}
		}
		if (lastDate >= 0){
			_dialogsOffsetDate = lastDate;
			_dialogsOffsetId = lastMsgId;
			_dialogsOffsetPeer = __makeInputPeer(lastPeer);
		}
	}
}

@InokentiyVas
Copy link

hexiaoyuan, hello.
What code in __makeInputPeer?

@hosseinGanjyar
Copy link

@hexiaoyuan Hi bro,
What is OffsetPeer? Where is it? How do I get it?
I have to get all dialogs.
Can anyone help me?

@mkellerman
Copy link
Contributor

Same here. Can someone provide a simple loop code example of generating all the UserDialogs?

@hosseinGanjyar
Copy link

@hexiaoyuan Show code of __makeInputPeer, please.

@hexiaoyuan
Copy link

hexiaoyuan commented Apr 6, 2019

@hexiaoyuan Show code of __makeInputPeer, please.
//
//

//
public TeleSharp.TL.TLAbsInputPeer makeInputPeer(TeleSharp.TL.TLAbsPeer peer)
        {
            if (peer is TeleSharp.TL.TLPeerChannel)
            {
                long access_hash = 0;
                var p = (TeleSharp.TL.TLPeerChannel)peer;
                Int64 tg_id = p.ChannelId | (new TeleSharp.TL.TLChannel()).Constructor << 32;
                var px = (TeleSharp.TL.TLChannel)(this.m_InnnerCachedDB.get_chat(tg_id));
                if (px != null && px.AccessHash.HasValue)
                    access_hash = px.AccessHash.Value;
                return new TeleSharp.TL.TLInputPeerChannel() { ChannelId = p.ChannelId, AccessHash = access_hash };
            }
            else if (peer is TeleSharp.TL.TLPeerChat)
            {
                var p = (TeleSharp.TL.TLPeerChat)peer;
                return new TeleSharp.TL.TLInputPeerChat() { ChatId = p.ChatId };
            }
            else if (peer is TeleSharp.TL.TLPeerUser)
            {
                long access_hash = 0;
                var p = (TeleSharp.TL.TLPeerUser)peer;
                Int64 tg_id = p.UserId | (new TeleSharp.TL.TLUser()).Constructor << 32;
                var px = (TeleSharp.TL.TLUser)(this.m_InnnerCachedDB.get_user(tg_id));
                if (px != null && px.AccessHash.HasValue)
                    access_hash = px.AccessHash.Value;
                return new TeleSharp.TL.TLInputPeerUser() { UserId = p.UserId, AccessHash = 0 };
            }
            else
            {
                return new TeleSharp.TL.TLInputPeerEmpty();
            }
        }


//
// NOTE: m_InnnerCachedDB is my private InMemoryDB
// private inner_cache_tg_db m_InnnerCachedDB = new inner_cache_tg_db();
//
private class inner_cache_tg_db
        {
            private Dictionary<Int64, (DateTime, TeleSharp.TL.TLContact)> m_contacts = new Dictionary<long, (DateTime, TeleSharp.TL.TLContact)>();
            private Dictionary<Int64, (DateTime, TeleSharp.TL.TLAbsUser)> m_users = new Dictionary<long, (DateTime, TeleSharp.TL.TLAbsUser)>();
            private Dictionary<Int64, (DateTime, TeleSharp.TL.TLAbsChat)> m_chats = new Dictionary<long, (DateTime, TeleSharp.TL.TLAbsChat)>();
            private Dictionary<Int64, (DateTime, TeleSharp.TL.TLDialog)> m_dialogs = new Dictionary<long, (DateTime, TeleSharp.TL.TLDialog)>();

            public string get_peer_title(int peer_id, string peer_type)
            {
                if(peer_type.Equals("User"))
                {
                    Int64 tg_id = peer_id | (new TeleSharp.TL.TLUser()).Constructor << 32;
                    if (m_users.ContainsKey(tg_id))
                    {
                        return displaynameFromTLUser(m_users[tg_id].Item2);
                    }
                }
                if(peer_type.Equals("Channel"))
                {
                    Int64 tg_id = peer_id | (new TeleSharp.TL.TLChannel()).Constructor << 32;
                    if (m_chats.ContainsKey(tg_id))
                    {
                        var chat = m_chats[tg_id].Item2 as TeleSharp.TL.TLChannel;
                        if (chat != null)
                            return chat.Title;
                    }
                }
                if(peer_type.Equals("Chat"))
                {
                    Int64 tg_id = peer_id | (new TeleSharp.TL.TLChat()).Constructor << 32;
                    if (m_chats.ContainsKey(tg_id))
                    {
                        var chat = m_chats[tg_id].Item2 as TeleSharp.TL.TLChat;
                        if (chat != null)
                            return chat.Title;
                    }
                }
                return "";
            }

            public void contacts_add(TeleSharp.TL.TLContact c)
            {
                m_contacts[tg_id_in_db(c)] = (DateTime.Now, c);
            }
            public bool is_contact_need_refresh(TeleSharp.TL.TLContact c)
            {
                Int64 tg_id = tg_id_in_db(c);
                if (!m_contacts.ContainsKey(tg_id)) return true;
                var dt = m_contacts[tg_id].Item1;
                if ((DateTime.Now - dt).TotalSeconds > 60 * 60 * 4) return true;
                return false;
            }
            public TeleSharp.TL.TLContact get_contact(Int64 tg_id)
            {
                if (!m_contacts.ContainsKey(tg_id)) return null;
                return m_contacts[tg_id].Item2;
            }
            public void users_add(TeleSharp.TL.TLAbsUser u)
            {
                m_users[tg_id_in_db(u)] = (DateTime.Now, u);
            }
            public bool is_user_need_refresh(TeleSharp.TL.TLAbsUser u)
            {
                Int64 tg_id = tg_id_in_db(u);
                if (!m_users.ContainsKey(tg_id)) return true;
                var dt = m_users[tg_id].Item1;
                if ((int)(DateTime.Now - dt).TotalSeconds > 86400) return true;
                return false;
            }
            public TeleSharp.TL.TLAbsUser get_user(Int64 tg_id)
            {
                if (!m_users.ContainsKey(tg_id)) return null;
                return m_users[tg_id].Item2;
            }
            public void chats_add(TeleSharp.TL.TLAbsChat c)
            {
                m_chats[tg_id_in_db(c)] = (DateTime.Now, c);
            }
            public bool is_chat_need_refresh(TeleSharp.TL.TLAbsChat c)
            {
                Int64 tg_id = tg_id_in_db(c);
                if (!m_chats.ContainsKey(tg_id)) return true;
                var dt = m_chats[tg_id].Item1;
                if ((int)(DateTime.Now - dt).TotalSeconds > 86400) return true;
                return false;
            }
            public TeleSharp.TL.TLAbsChat get_chat(Int64 tg_id)
            {
                if (!m_chats.ContainsKey(tg_id)) return null;
                return m_chats[tg_id].Item2;
            }
            public void dialogs_add(TeleSharp.TL.TLDialog d)
            {
                m_dialogs[tg_id_in_db(d)] = (DateTime.Now, d);
            }
            public bool is_dialog_need_refresh(TeleSharp.TL.TLDialog d)
            {
                Int64 tg_id= tg_id_in_db(d);
                if (!m_dialogs.ContainsKey(tg_id)) return true;
                var dt = m_dialogs[tg_id].Item1;
                if ((int)(DateTime.Now - dt).TotalSeconds > 86400) return true;
                return false;
            }
            public TeleSharp.TL.TLDialog get_dialog(Int64 tg_id)
            {
                if (!m_dialogs.ContainsKey(tg_id)) return null;
                return m_dialogs[tg_id].Item2;
            }
            public IList<TeleSharp.TL.TLDialog> get_dialogs()
            {
                IList<TeleSharp.TL.TLDialog> ret = new List<TeleSharp.TL.TLDialog>();
                foreach (var v in m_dialogs.Values)
                {
                    ret.Add(v.Item2);
                }
                return ret;
            }
        }

//
// NOTE AGAIN, you should TryGetContacts first and save info to your m_InnnerCachedDB.
// 
// var req = new TLRequestGetContacts() { Hash = "" };
// await SendRequestAsync<TLContacts>(req);
//

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants