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
Related
I currently use the function get_users_following(user_id_account, max_results=1000) to get the list of follows of an account, to know who he follows on twitter. So far it works well as long as the user follows less than 1000 people because the API limits to a list of maximum 1000 users. The problem is that when he follows more than 1000 people I can't get the last people. The function always gives me the first 1000 and ignores the last ones
https://docs.tweepy.org/en/stable/client.html#tweepy.Client.get_users_followers
https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-following
There is a pagination_token parameter but I don't know how to use it. What I want is just the last X new people followed so I can add them to a database and get a notification for each new entry
client = tweepy.Client(api_token)
response = client.get_users_following(id=12345, max_results=1000)
Is it possible to go directly to the last page?
Tweepy handles the pagination with the Paginator class (see the documentation here).
For example, if you want to see all the pages, you could do something like that:
# Use the wait_on_rate_limit argument if you don't handle the exception yourself
client = tweepy.Client(api_token, wait_on_rate_limit=True)
# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)
for response_page in paginator:
print(response_page)
Or you could also directly get the full list of the user's followings:
# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)
for user in paginator.flatten(): # Without a limit argument, it gets all users
print(user.id)
I am making a bot that has a database of things and sometimes someone would want to see what things a different person has, this would be possible by using discord id, but I just don't was everyone sending their discord ids around.
I was trying to use something like this:
def get_user_id(name_en, tag_en, guild_id):
guild = client.get_guild(guild_id)
user = discord.utils.get(guild.members, name= "Atom" , discriminator= "#1803")
print(user)
But that just outputs an empty message.
So is there a way to get a discord id from a username?
Also is this possible with making the discord bot in a different programming language? (if not possible in discord.py)
Thanks
You can use Guild.get_member_named()
The name can have an optional discriminator argument, e.g. “Jake#0001” or “Jake” will both do the lookup. However the former will give a more precise result. Note that the discriminator must have all 4 digits for this to work.
def get_user_id(name_en, tag_en, guild_id):
guild = client.get_guild(guild_id)
user = guild.get_member_named(name_en)
print(user)
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))
So I'm attempting to write a raiding bot for my raiding discord using the discord.py library in python. This scripts is supposed to be forming a list of members in the voice channel for raiding. For some reason, this script is not working. Whenever memids is printed, it just prints an empty list.
If anyone is familiar with discord.py and could tell me why this isn't working, please do. It's really troubling me and I have tried everything in my knowledge to fix it.
#find raiding
voice_channel = discord.utils.get(ctx.message.server.channels, id = '440014722587426816')
#finds the members
members = voice_channel.voice_members
memids = []
for member in members:
memids.append(member.id)
print(memids)
If you know the id of the channel you can do it this way. Works for me :D
channel = client.get_channel(1234567890) #gets the channel you want to get the list from
members = channel.members #finds members connected to the channel
memids = [] #(list)
for member in members:
memids.append(member.id)
print(memids) #print info
I faced the same problem. voice_channel.members would return either empty or incomplete lists some times.
The docs say:
voice_states
Returns a mapping of member IDs who have voice states in this channel.
Note: This function is intentionally low level to replace members when the member cache is unavailable.
https://discordpy.readthedocs.io/en/latest/api.html#voicechannel
I guess the members can't be trusted to return accurate connected member list consistently.
I solved this problem with the following code:
member_ids = voice_channel.voice_states.keys()
You need to enable "SERVER MEMBERS INTENT:
If your bot tracks server members or downloads the entire member list, you may need the server members intent to receive member events and the member list." on the discord developer page on the bot tab.
There isn't much to go on from your question. I believe your problem is that the id that you provided to utils.get(...) isn't the correct id of the voice channel. This is probably the reason to why you're always getting an empty list.
voice_members
A list of Members that are currently inside this voice
channel. If type is not ChannelType.voice then this is always an empty
array.
If you're not fully sure about the voice channel's actual id, I suggest you search by the name and type (discord.ChannelType.voice):
voice_channel = discord.utils.get(ctx.message.server.channels, name="channelname", type=discord.ChannelType.voice)
I you know the channel id, I suggest using
voice_channel = client.get_channel(channel_id)
instead (documentation here). If you're using discord.py-rewrite, you can also use:
voice_client = ctx.guild.get_channel(channel_id)
if the channel you're looking for is in the context guild (documentation here).
I am trying to learn how to use the new WebHooks service on IFTTT, and I'm struggling to figure out how it is supposed to work. Most of the projects I can find online seem to refer to a deprecated "maker" service, and there are very little resources for interpreting the new channel.
Let's say I want to check the value of "online" every ten minutes in the following json file: https://lichess.org/api/users/status?ids=thibault
I can write a Python script that extracts this value like so:
response = urlopen('https://lichess.org/api/users/status?ids=thibault')
thibault = response.read()
data = json.loads(thibault)
status = data[0]['online']
If the status is equal to "true", I would like to receive a notification via email or text message. How do I integrate the python script and the webhooks service? Or do I even need to use this script at all? I assume I need some kind of cron job that regularly runs this Python script, but how do I connect this job with IFTTT?
When I create a new applet on IFTTT I am given the ability to create a trigger with a random event name, but it's unclear what that event name corresponds to.
I have a similar setup for my IFTTT webhook service. To the best of my understanding, the answer to your question is yes, you need this script (or similar) to scrap the online value, and you'll probably want to do a cron job (my approach) or keep the script running (wouldn't be my preference).
IFTTT's webhooks a json of up to 3 values, which you can POST to a given event and key name.
Below is a very simple excerpt of my webhook API:
def push_notification(*values, **kwargs):
# config is in json format
config = get_config()
report = {}
IFTTT = {}
# set default event/key if kwargs are not present
for i in ['event', 'key']:
IFTTT[i] = kwargs[i] if kwargs and i in kwargs.keys() else config['IFTTT'][i]
# unpack values received (up to 3 is accepted by IFTTT)
for i, value in enumerate(values, 1):
report[f"value{i}"] = value
if report:
res = requests.post(f"https://maker.ifttt.com/trigger/{IFTTT['event']}/with/key/{IFTTT['key']}", data=report)
# TODO: add try/except for status
res.raise_for_status()
return res
else:
return None
You probably don't need all these, but my goal was to set up a versatile solution. At the end of the day, all you really need is this line here:
requests.post(f"https://maker.ifttt.com/trigger/{event}/with/key/{key}", data={my_json_up_to_3_values})
Where you will be placing your webhook event name and secret key value. I stored them in a config file. The secret key will be available once you sign up on IFTTT for the webhook service (go to your IFTTT webhook applet setting). You can find your key with a quick help link like this: https://maker.ifttt.com/use/{your_secret_key}. The event can be a default value you set on your applet, or user can choose their event name if you allow.
In your case, you could do something like:
if status:
push_notification("Status is True", "From id thibault", event="PushStatus", key="MysEcR5tK3y")
Note: I used f-strings with version 3.6+ (It's great!), but if you have a lower version, you should switch all the f-strings to str.format().