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.
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
I save ids of all users of my bot in a database but how can I get their current username if I know id? I'm using Python 3 and Telepot framework.
Information about a user from user_id can be obtained by the getChat method in telepot.
bot = telepot.Bot(TOKEN)
info = bot.getChat(user_id)
print(info)
you can get the inforamtion of an account by checking the entity.
You can try to get the entity with this method:
entity = client.get_entity(chat_id)
then Print the entity and try to get the name from there with something like name = entity.chat.name
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.
I have the following code in Python to send a message to myself from a bot.
import requests
token = '123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI'
method = 'sendMessage'
myuserid = 1949275XX
response = requests.post(
url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
data={'chat_id': myuserid, 'text': 'hello friend'}
).json()
print(response)
but this returns {'description': 'Bad Request: chat not found', 'error_code': 400, 'ok': False}
What am I doing wrong? I got myuserid by sending /getid to #myidbot and I got my token from #BotFather
As #maak pointed out, you need to first send a message to the bot before the bot can send messages to you.
I was using prefix # before the value of chat_id as suggested everywhere. I removed it and it started working.
Note: if your chat id is 12345678 then you need to prefix it with -100 such that it is -10012345678.
Example Postman call:
/sendMessage?chat_id=-10012345678&text=Let's get together
If your trying to send messages to a group, you must add a ‘-‘ in front of your chat ID.
For example:
TELEGRAM_REG_CHAT_ID="1949275XX"
should be
TELEGRAM_REG_CHAT_ID="-1949275XX"
There is a way to send notifications messages to telegram. It's a bit tricky but the tutorial is great!
http://bernaerts.dyndns.org/linux/75-debian/351-debian-send-telegram-notification
I just sended a message of my apache state to a privat channel.
Works also on public channel but it's not what i wantet. As you call a script (bash) you can prepare the parameters in any script language.
Hope that helps.
For me it worked only with # prefix before channel id
I had some trouble with this after upgrading to a supergroup. The chat_id was updated and it was a bit harder to find this new id.
In the end I solved this with this by following this comment: https://stackoverflow.com/a/56078309/14213187
If you use a username, it does not require any prefix. That means the following are incorrect:
https://t.me/vahid_esmaily_ie
t.me/vahid_esmaily_ie
And this is the correct case:
vahid_esmaily_ie
If you want to use a bot message to the channel, you can refer step here
Steps:
Create a Telegram public channel
Create a Telegram BOT (for example x_bot) via BotFather
Set the x_bot as an administrator in your channel
the chat_id is #x_bot, it's a part of https://t.me/x_bot that does not add your channel name.
Telegram bots can't send messages to user, if that user hasn't started conversation with bot yet, or bot is not present in chat (if it's a group chat). This issue is not related to the library, this is simply Telegram restriction, so that bots can't spam users without their permission.
you need to first send a message to the bot before the bot can send messages to you.