How to find username from id in telethon - python

I wanted to know that can I find the username of a public telegram channel from it's public id in telethon?
I tried the get_entity method but it is only working for telegram group not channel.
So, how can I get the username ?

Try this,
channel=-100XXXXXXX
result = client(functions.channels.GetFullChannelRequest(
channel=channel
))
print(result.chats[0].username)
channel id should start with -100.
To find the username of a public channel using channel id, You should be a member of that particular public channel.
But, In public Channels, You can see the username in their link https://t.me/channel_username. You don't have to be a member of a channel, to see the username.

Related

Get Channel Id discord with request (python)

def send_dm():
token = 'i know, just not putting my token here'
message = 'yo, dont mind just testing smth'
id = "681089525702721546"
header = {
'authorization' : token,
'user-agent' : 'i know, just not putting my user_agent here',}
payload = {'content' : message}
sd = requests.post(f"https://discord.com/api/v9/channels/{id}/messages", headers=header, data=payload)
print(sd.status_code)
So i am trying too send for fun too my friend a message with python requests but i just find out that you need to get the channel id for the direct message, but i dont know how, can someone help me finding out?
Go to Discord Settings
Click on Advanced under App Settings
Toggle Developer Mode
Go to the channel of your choice
Right-click and click on Copy-ID
Per documentation Create DM with a user (recipient_id) to return a Channel Object.
In this returned object the id key will be the id of this channel that you can use to send a message in the way you tried to.
If you want to send a message to a spesific/public channel you can get a list of all the channels via Get Guild Channels

Invite Discord.py

I am trying to read the members of a guild by invitation. As soon as we send a link from a server, information such as name, online membership and total members appears. How do I get this information? (The library in question is discord.py)
Example:
(I can't share an image, so I ask you to open the photo link:)
https://cdn.discordapp.com/attachments/842566116978327584/844421408019841034/unknown.png
Name: ☕|Clube do café #240|☕
Photo url: https://cdn.discordapp.com/icons/828004701148676137/62f745ee62f7fb6dd7fbc34a6b75f2df.png?size=128
Online: 112
Members: 232
Id: 806673124819992688
(I extracted this information manually, but was wondering how to do this in the code)
I have already tried to use the following message attributes: attachments, embeds, guild, stickers and system_content. Both returned empty or the invite link.
This is possible with the help of the fetch_invite() function
invite = await client.fetch_invite(url = "https://discord.gg/Invite-ID")
You can then retrieve your desired attributes
For example:
memberCount = invite.approximate_member_count
presenceCount = invite.approximate_presence_count
guildName = invite.guild.name
guildIcon = invite.guild.icon_url
guildID = invite.guild.id
And so on

problem with forward message method in telegram bot

I am trying to forward a message in telegram bot API in python . It shows "Bad Request: chat_id is empty" despite using same chat_id in sendMessage and works perfectly fine.
https://api.telegram.org/bot{BOT_TOKEN}/forwardMessage?chat_id={CHAT_ID}&from_chat_id={ID}&message_id={MID}
my problem is what is the message id and how can i find it?
for example chat id is like this number 123456789.
what does message id look like ?
You are a bot. So, people interact with you (directly or in a group). When a message is sent to your bot, you would receive a callback that includes the message details (including the chat_id, the sender chat id, the message id, and all other details). Something like this (see message->message_id and message->from_id):
{
"update_id":1111,
"message":{
"message_id":111,
"from":{
"id":1111,
"is_bot":false,
"first_name":"...",
"last_name":"...",
"username":"...",
},
"chat":{
"id":1111,
"first_name":"...",
"last_name":"...",
"username":"...",
"type":"private"
},
"date": 1111,
"text":"...."
}
}
So, you can store those details and use them to forward that message to another chat. In each chat (unique chat id), each message has a chat id (which is usually an incremental number) and by these two identifier, you can uniquely select which message from which chat should be forwarded.
Depends on where you want to access a chat_id, you can use different ways.
if you want to send a specific message to a user or bot that you don't know his chat_id, you can print a message from that user and access chat_id. Or simply forward a message from that user to #ShowChatIdBot and it returns you the related chat_id.
In fact this bot extract data from your forwarded message and gives back chat_id in that message to you.

Get members id from telegram channel - pyTelegramBotApi

I'm doing a bot, in which I want the user to start the bot to verify that the user of the telegram channel. I'm programming in python with the api telebot.
With this I get the user.id of the user:
idUser = str (message.from_user.id)
With this I have managed to obtain the number of users, but not the ids to do the verification:
members = tb.get_chat_members_count ("# ChannelName")
according to the documentation all api methods are callable but modified to python standards so it would become
memberobject = tb.get_chat_member(channel,userid)

How to send message with #ID telegram using telethon library

I want send message with telethon but i dont have phone number this .
i have only #username Telegram.
with this code i can send message for my contact phone :
result = client.invoke(ImportContactsRequest([contact], replace=True))
contacts = client.invoke(GetContactsRequest(""))
for u in result.users:
client.send_message(u, 'Hi')
But i want send message to #username Telegram
You can just do the following now:
client.send_message('username', 'hello')
Old answer:
It's on the Project's wiki, quoted below.
Via ResolveUsernameRequest
An "entity" is used to refer to either an User or a Chat (which includes a Channel). Perhaps the most straightforward way to get these is by resolving their username:
from telethon.tl.functions.contacts import ResolveUsernameRequest
result = client.invoke(ResolveUsernameRequest('username'))
found_chats = result.chats
found_users = result.users
# result.peer may be a PeerUser, PeerChat or PeerChannel
See Peer for more information about this result.

Categories