How to check if my account already in chat, channel, private channel, private group? - telethon

I want to check if my account is member of the channel. How can I do this if I only have valid invite link

With the current telethon API, you can't check if you're a member of a channel or private group using an invite link. However, you can resolve the invite link into channel ID and then use iter_dialogs to check if there is a same ID.
To avoid FloodWait, it's better to do iter_dialogs once and save a local copy for yourself. You may update your local copy when you join new channels and groups.
For public groups you can iter_participants and find yourself. However, the former method still works here as well.

You can do:
async for dialog in client.iter_dialogs():
if dialog.entity.megagroup:
group_name = dialog.entity.title
members_count = str(dialog.entity.participants_count)
print(f'GROUP: {group_name} - ({members_count} members)')
Here I checked if the chat is a megagroup, if so then it will print participants count and group's name.

Related

TL-Schema - how to get access_hash of private channel?

To send messages (messages.sendMessage), you need channel_id and access_hash. For any public channels or chats, I can get it using contacts.resolveUsername and pass the username. But what about private channels, Is it accessible only by link? I Can't find a solution. Thanks!
You can get it either by joining the channel and checking the retval or iterating your dialogs (client.iter_dialogs)

How to look up YouTube Live Chat Ban ID to delete it

The YouTube Live Streaming API has the ability to ban and un-ban a user from chat and provides the following method specifically to perform the un-banning:
https://developers.google.com/youtube/v3/live/docs/liveChatBans/delete
The API requires you to pass the id parameter that identifies the chat ban to remove and states that the value uniquely identifies both the ban and the chat. However, there does not appear any way to look up the chat bans for a channel, other than when you get the ID back initially when the ban occurs.
So is there no way to un-ban a user via the API long after the ban has occurred and you no longer have the ban ID?
Actually, LiveChatBanId is a base64-encoded string containing
banned user channel id
owner channel id
some random bytes?
Here is some python code that will generate it for you
import base64
banned_user_id = 'banned user id'
owner_channel_id = 'channel owner id'
decoded = f"\x08\x01\x12\x18{banned_user_id}\x1a8\n\r\n\x0b7cHT2DHCA7s*'\n\x18{owner_channel_id}\x12\x0b7cHT2DHCA7s".encode('utf-8')
encoded = base64.b64encode(decoded)
print(encoded)
Fill in banned_user_id and owner_channel_id
You may need to invoke the "LiveChatBans: delete" method twice, that's how Google works

How to forward a message in Telegram API

There are 2 methods in Telegram API that forward message:
messages.forwardMessage
messages.forwardMessages
I want to use forwardMessage method to forward a message from a channel, group or user to another one. Definition of this method is:
messages.forwardMessage#33963bf9 peer:InputPeer id:int random_id:long = Updates;
As you see this method has 3 input parameters:
peer that represents the channel, group or user that we forward message to. (Destination)
id that is message_id.
random_id that has internal use.
As we know the message_id is a unique number in a chat. so a message_id in a group has refers to a message that differs with the same message_id in other group.
So the main question is that how we determine the source peer of forwarding? Because the source peer is not determined by message_id.
P.S: My question is about methods in Telegram API, not Telegram Bot API.
There seems to an issue with ForwardMessageRequest which doesn't specify the source chat. Obviously message_id is not unique and through my tests I noticed wrong messages will be forwarded by just specifying the message_id. And I noticed message_id is not unique.
But the issue doesn't exist with ForwardMessagesRequest. Following is an example how to use the ForwardMessagesRequest version.
Forwarding Example:
Here is the code I used for testing (I am using Telethon for python, but it won't matter since it's directly calling telegram API):
source_chat = InputPeerChannel(source_chat_id, source_access_hash)
total_count, messages, senders = client.get_message_history(
source_chat, limit=10)
for msg in reversed(messages):
print ("msg:", msg.id, msg)
msg = messages[0]
print ("msg id:", msg.id)
dest_chat = InputPeerChat(dest_chat_id)
result = client.invoke(ForwardMessagesRequest(from_peer=source_chat, id=[msg.id], random_id=[generate_random_long()], to_peer=dest_chat))

Obtaining chat_id having chat link in Telegram API

I am using Telegram API to develop a program to join Telegram groups or channel by their links.
Methods that join group or channel (e.g. channels.joinChannel) need chat_id or channel_id, but I have only the links of the groups or channels (e.g. #channel_username or https://t.me/channel_username or https://t.me/joinChat/xxxxx)
How can I obtain chat_id or channel_id of a group or channel having its link?
P.S: I'm not the admin of these groups or channels.
I found the answer:
First we must use checkChatInvite method. It uses the chat link as input parameter and outputs the chat specifications includes chat_id.
Then we use joinChat method method. it uses the chat_id got from the previous step and joins to that group or channel.
Selected answer seems to be outdated. In recent versions there is checkChatInviteLink call, but it requires the chat url to start with https://t.me/joinchat/
If you want to resolve a link like https://t.me/chatname, you can use searchPublicChat API call.
This works for me (using https://github.com/alexander-akhmetov/python-telegram):
def resolve_channel_link(tg, link):
if link.startswith('https://t.me/'):
link = link.split('/')[-1]
else:
raise RuntimeError('cant parse link', link)
r = tg.call_method('searchPublicChat', [{'username', link}])
r.wait()
if r.error:
raise RuntimeError(r.error_info)
assert(r.update['#type'] == 'chat')
return r.update['id']

How to get private messages related to a particular user programatically?

I want to get private messages (provided by the Privatemsg module) that are related to a particular user, programatically.
How can I do this?
Private messages are entities so you can use entity_load() to load them by condition...or use the wrapper provided by the private message module itself:
$messages = privatemsg_message_load_multiple(array(), array('author' => $uid));
That will get all messages created by the user identified by $uid.