tomer8007 / kik-bot-api-unofficial

Python API for writing unoffical Kik bots that act like humans
MIT License
126 stars 76 forks source link

Need peer jid using username #206

Closed achintya10 closed 2 years ago

achintya10 commented 2 years ago

Hi

I need to fetch the peer jid for an user , using the username(kik username). Can you tell me how to do that? My aim is to ban an user using the username(e.g. achintya10) by the admin of a group. And when that user joins that group, the bot will automatically remove the user from that group.

Please help me out as I need this to implement it asap.

StethoSaysHello commented 2 years ago

To get a JID with a username, use get_jid() _(from client.py)_ Here is an example:

username = "example" #Give a username. Can be with or without an "@"
def get_jid(username):
    try:
        grab_jid = self.client.get_jid(username)  # Attempts to get the JID
        return grab_jid
    except:
        return False
jid = get_jid(username)
attempts = 1
while jid == False:  # if there was an problem getting the JID, this continues retrying until it works
    if attempts > 5:  # Limits the number of attempts to fetch the JID to 5 so you don't get stuck in an error loop
        print("I was unable to get the JID for \"" + username + "\"! Please try again.\n(Make sure the username is valid!)")
        jid = ""  # Break the while loop
    else:
        jid = get_jid(username)  # Tries again
        attempts = attempts + 1
print(jid) #Do something with the JID.

You should add some more specific error handling in a fork though! (Such as catching KeyError when the username is invalid.)

I made a "ban [username]" bot example for you to reference here. As for removing a particular user when they join a group, you wouldn't need to do this if they are already banned?! But in theory you could store the JID in a list somewhere and whenever a group status message is received, remove any users in that list.

Hope this helps!

(Edit: Added more specifics.)

achintya10 commented 2 years ago

Hi

Thanks for your quick reply.

Questions:

  1. Why need to try 5 times?
  2. If it fails to get the 'jid' after 5 attempts, what next?

Thanks again.

Sitiaro commented 2 years ago

Hi

I need to fetch the peer jid for an user , using the username(kik username). Can you tell me how to do that? My aim is to ban an user using the username(e.g. achintya10) by the admin of a group. And when that user joins that group, the bot will automatically remove the user from that group.

Please help me out as I need this to implement it asap.

For the 'removing the user when they join a group', add this to on_group_status_recieved

global c_jid, cd_jid c_jid = response.status_jid cd_jid = response.group_jid self.client.add_friend(response.status_jid) return

And on peer_info_revieved, add this-

global c_jid, cd_jid

Now make the bot write the users it grabs to a text file and make it check the file whenever someone joins. The latter will be done on on_peer_info_recieved bc that'll be checked whenever a new user joins.

I've my own bot and it has a similar feature. I won't share the source code but feel free to dm me @ferns on kik if you wanna see how it works.

StethoSaysHello commented 2 years ago
  1. Why need to try 5 times?
  2. If it fails to get the 'jid' after 5 attempts, what next?
  1. I try 5 times because the attempt to get a JID sometimes times out _(from TimeoutError, refer to line 679 of client.py)_, or the username may be invalid, which I mentioned you should attempt to catch in KeyError.

  2. If all 5 attempts fail, the code I provided will print an error message in the terminal and stop trying. The "ban [username]" example sends an error message in the group.

achintya10 commented 2 years ago

Hi I need to fetch the peer jid for an user , using the username(kik username). Can you tell me how to do that? My aim is to ban an user using the username(e.g. achintya10) by the admin of a group. And when that user joins that group, the bot will automatically remove the user from that group. Please help me out as I need this to implement it asap.

For the 'removing the user when they join a group', add this to on_group_status_recieved

global c_jid, cd_jid c_jid = response.status_jid cd_jid = response.group_jid self.client.add_friend(response.status_jid) return

And on peer_info_revieved, add this-

global c_jid, cd_jid

Now make the bot write the users it grabs to a text file and make it check the file whenever someone joins. The latter will be done on on_peer_info_recieved bc that'll be checked whenever a new user joins.

I've my own bot and it has a similar feature. I won't share the source code but feel free to dm me @ferns on kik if you wanna see how it works.

I am now writing to a database and storing the group_jid , username and status_jid. But status jid seems different on all occasions so I am little confused, what to store in database and if I am storing the correct data in database tables. While writing ban user in the function on_group_message_received, I am getting the chat_message.from_jid,, which is not the jid of the user being banned, rather I think it is the peer id of the admin who is banning the user. Let me know your thoughts on this.

Thanks again.

Sitiaro commented 2 years ago

Hi I need to fetch the peer jid for an user , using the username(kik username). Can you tell me how to do that? My aim is to ban an user using the username(e.g. achintya10) by the admin of a group. And when that user joins that group, the bot will automatically remove the user from that group. Please help me out as I need this to implement it asap.

For the 'removing the user when they join a group', add this to on_group_status_recieved global c_jid, cd_jid c_jid = response.status_jid cd_jid = response.group_jid self.client.add_friend(response.status_jid) return And on peer_info_revieved, add this- global c_jid, cd_jid Now make the bot write the users it grabs to a text file and make it check the file whenever someone joins. The latter will be done on on_peer_info_recieved bc that'll be checked whenever a new user joins. I've my own bot and it has a similar feature. I won't share the source code but feel free to dm me @ferns on kik if you wanna see how it works.

I am now writing to a database and storing the group_jid , username and status_jid. But status jid seems different on all occasions so I am little confused, what to store in database and if I am storing the correct data in database tables. While writing ban user in the function on_group_message_received, I am getting the chat_message.from_jid,, which is not the jid of the user being banned, rather I think it is the peer id of the admin who is banning the user. Let me know your thoughts on this.

Thanks again.

Will it be possible for you to dm me on kik? I'll give you a reference and that should make things way easier. My kik is @ferns

achintya10 commented 2 years ago

Sorry , my smartphone is not working right, so can't use kik.

StethoSaysHello commented 2 years ago

While writing ban user in the function on_group_message_received, I am getting the chat_message.from_jid,, which is not the jid of the user being banned, rather I think it is the peer id of the admin who is banning the user. Let me know your thoughts on this.

The chat_message.from_jid will usually give you an AJID (looks like gibberish), not a normal JID. Sitiaro's script attempts to add the user as a friend to get their JID I believe. In your shoes I would just keep it simple and attempt to remove all JIDs in the list each time. Seems odd to try every time, but this is faster and has a higher success rate than trying to grab a JID.

Example:

blacklist = ["example_7t5@talk.kik.com", "example2_u67@talk.kik.com"]
#^ Put the JIDs of the people you don't want in the group here. 
#^ You could make something to add/remove JIDs from this list with commands. Can be stored/pulled from a text file like Sitiaro mentioned too.

def on_group_status_received(self, response: chatting.IncomingGroupStatus):
    print("[+] Status message in {}: {}".format(response.group_jid, response.status))
    if 'has joined the group' in response.status:  # Only triggered when someone joins the group
        for jid in blacklist:  # Loops through each JID in the blacklist
            self.client.remove_peer_from_group(response.group_jid, jid)  # Attempts to remove blacklisted user if they are in the group.
            time.sleep(0.3)  # Waits for a split second so that you don't get an error on the next attempt.

Hope this helps, I apologize for the mixed messages in here.

_(Edit: Oops, forgot the GJID in remove_peer_from_group. Corrected.)_

achintya10 commented 2 years ago

Hi

The above code looks promising. I will try and let you know if that helps.

Sitiaro commented 2 years ago

Hi

The above code looks promising. I will try and let you know if that helps.

Actually, you can do that too @achintya10 . A more efficient way would be to make a command to grab jids from usernames like the one in Stetho's example script and write it to a database, and read it to check the jid of the person when they join. That'd be way simpler and equally efficient.

achintya10 commented 2 years ago

Hi

Thanks for all the help. The code is working now.