I get a Channel Invalid error using telethon - python

I am trying to get Users from my Telegram Group to my other Telegram Group and using this code:
group_entity = InputPeerChannel(group.id, group.access_hash)
client(InviteToChannelRequest(channel=group_entity, users=[user_to_add]))
A few weeks before this code worked perfectly fine but now I get this error:
telethon.errors.rpcerrorlist.ChannelInvalidError: Invalid channel object. Make sure to pass the right types, for instance making sure that the request is designed for channels or otherwise look for a different one more suited (caused by InviteToChannelRequest)
What am I doing wrong?

you must do like this:
group_entity = await client.get_entity(InputPeerChannel(group.id, group.access_hash))

Related

Why do I get ERROR - TeleBot: "Infinity polling exception: 'list' object has no attribute 'id' specifically when I use new_chat_members?

I made a bot and invited it to a group.
The bot is administrator of this group, it's not private. I'm trying to make the bot send private messages to users that join the group.
The problem is that when I try to do so, I get following error:
ERROR - TeleBot: "Infinity polling exception: 'list' object has no attribute 'id'
and I cant figure out what that means, or why I get it or how to fix it.
This is my code:
import telebot
bot = telebot.TeleBot('here is bot token')
#bot.message_handler(content_types=["new_chat_members"])
def handler_new_member(message):
first_name = message.new_chat_members[0].first_name
bot.send_message(message.new_chat_members.id, "Something something".format(first_name))
bot.infinity_polling()
I tried to make the bot send private messages to new members and expected it to read the id of new members, which as far as I understand is possible.
I'm new to python and telegram api. I appreciate any help.
It seems that new_chat_members is an actual list. In the first line you're using list index to get the user's first name. Lists in Python do not have attribute id. Maybe you meant new_chat_members[index].id?

How to add user to group using Telethon (Telegram python library) InviteToChannelRequest

I'm using the Telethon python library for Telegram and am currently trying to add users to my group using the following: https://docs.telethon.dev/en/latest/examples/chats-and-channels.html#adding-someone-else-to-such-chat-or-channel
However, I run into a problem with the InviteToChannelRequest function whose signature is the following:
channels.inviteToChannel#199f3a6c channel:InputChannel users:Vector<InputUser> = Updates
As you can see, the channel should be of type InputChannel however when I try to create an InputChannel to pass into the function using it's constructor, which is the following:
inputChannel#afeb712e channel_id:int access_hash:long = InputChannel
I do not have the group's access_hash so I'm unable to create a InputChannel to pass into the InviteToChannelRequest function.
So how exactly do I either get the access_hash of the group I want to add users to or how do I use the InviteToChannelRequest function?
If I try to create an InputChannel without the access_hash, this is the error I get:
Thank you!
It turns out that the group I had created was set to private so I went on the telegram app and set it to public and after I did that, I ran this snippet:
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
Which listed the details of the channel I wanted along with it's access_hash

Creating colored roles in discord through discord.py gives error

I want to make coloured roles with the help of a command, so that whenever I do .cr the bot creates the roles specified in the code. For example, my code is:
async def cr(self, ctx):
guild = self.client.get_guild(783547639944839178)
channel = self.client.get_channel(809683610058752010)
await guild.create_role(name="red", colour=discord.Colour(2452332))
await guild.create_role(name="skyblue", colour=discord.Colour(17167187))
await guild.create_role(name="yellow", colour=discord.Colour(24821016))
await channel.send("Done.")
Firstly, when I do this command, it does create a role named red; but its colour is "#256b6c" instead of "#F51720". I used an online hex to RGB converted to convert these values.
Second, it doesn't execute the next line and gives this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In color: int value should be less than or equal to 16777215.
I used the same converter again. Where am I going wrong?
Any help would be highly appreciated!
I think the error pretty much speaks for itself, the values that you're passing are invalid, if you try converting the integers you get the next hex values
>>> hex(2452332)
0x2566b6c
>>> hex(17167187)
0x105f353
>>> hex(24821016)
0x17abd18
As you can see instead of being 6 digits long they're of length 7 (not including the 0x). I'm not entirely sure what colors you're trying to get but they're invalid. If you want to create a role knowing it's hex value:
color = int("ff0000", 16)
# Or
color = 0xff0000
await guild.create_role(..., colour=color)
Both solutions will work
Integers are stored on 8bits in the memory, and the number you try to pass doesn't fit on those 8bits. At least that's what the error says. To fix your problem, if you have access to the variable, cast it to something else or try to find another way to pass the color... I'd go for the second solution, specially if you don't get the right color ;)

value error while trying to join a channel using telethon

I have a code that I need to join a given channel in but getting the following error
telethon.errors.rpcerrorlist.ChannelInvalidError: Invalid channel object. Make sure to pass the right types, for instance making sure that the request is designed for channels or otherwise look for a different one more suited (caused by JoinChannelRequest)
my code :
group_input = InputChannel(group.id, group.access_hash)
client(JoinChannelRequest(group_input))
here I'm using inputChannel because i looked in the joinChannelRequest and it asked for "TypeInputChannel"
and it's a union of [InputChannelEmpty,InputChannel,InputChannelFromMessage]
another things that I tried:
1-
client(JoinChannelRequest(group.id))
2-
client(JoinChannelRequest(group))
3-
group_input = InputPeerChannel(group.id, group.access_hash)
client(JoinChannelRequest(group_input))
The simplest way you can do is to retrieve the username of the public channel
Suppose the username of the channel is #The_Channel
Get the entity
entity = client.get_entity("t.me/The_Channel") #omit #
Then invoke this function
client(JoinChannelRequest(entity))

Get all of the users from a specific channel Slack API

I am trying to make a Slack Bot using python, and I have a issue, I am not able to get the users from a specific channel, I only succeed if I take all of the users. Basically I want only those from (eg. random channel).
Until now I tried to get the team's ID from every user and compare it to a channel ID, but that failed because everyone is having the same ID and I can't figure out why.
Here is the snippet of the code:
def users_of_the_channel():
global slack_client
#this is the variable which is initialized with SlackClient(BOT_TOKEN)
api_call = slack_client.api_call( "users.list",channel="C0XXXXXXX")
if api_call.get('ok'):
channels = api_call.get('members')
for channel in channels:
print ("this is cool : ", channel['team_id'])
The issue I believe is that when I initialize the api_call variable I call the function with the users.list argument, I tried with usergroups.list and usergroups.users.list but with no success.
Basically to keep it short I need the list with the users from a channel, and the documentation hasn't helped me.
users.list does not take channel id as input, which you are providing in your API call.
Slack provide different bot token for different teams.
Simply call api with bot token of team of which you require user list and you will get the list of members.
client = SlackClient(SLACK_BOT_TOKEN)
request = client.api_call("users.list")
if request['ok']:
for item in request['members']:
print item['name']
For more details about optional arguments you can provide with api call, refer documentation(https://api.slack.com/methods/users.list).
You probably can use the new conversations API to retrieve that (specifically conversations.members endpoint):
https://api.slack.com/methods/conversations.members

Categories

Resources