Get Started - Learn How To Make Your Bot!
List of all currently Free Items
Have you ever wanted to check the profile information of an user without leaving the room? Well, now you can do it!
Today I will show a very simple code to retrieve a given user’s number of followers, friends, following, posts, last login, and when the account was created, all in the chat!
Highrise 2023-07-27 00-18-45.mp4
Here’s what we will need to import into our bot:
from highrise import *
from highrise.models import *
from highrise.webapi import *
from highrise.models_webapi import *
To this code I will be using the 📬 Command Handler so for that I will create a file called userinfo.py
in my functions folder:
Since the code for this function is pretty simple I won’t be detailing very much on what every line does, the code will be commented but here’s a resume: The when saying “/userinfo <username>”
in the room, the bot will make requests to the webapi api to get the user’s id, profile info, and posts. Based on the response from the api we will get some specific attributes and make the bot say them on chat.
from highrise import *
from highrise.models import *
from highrise.webapi import *
from highrise.models_webapi import *
async def userinfo (self: BaseBot, user: User, message: str) -> None:
#Split the message into parts
parts = message.split(" ")
if len(parts) != 2:
await self.highrise.chat("Incorrect format, please use /userinfo <@username>")
return
#Removes the @ from the username if it exists
if parts[1].startswith("@"):
username = parts[1][1:]
else:
username = parts[1]
#Get the user id from the username
user = await self.webapi.get_users(username = username, limit=1)
if user:
user_id = user.users[0].user_id
else:
await self.highrise.chat("User not found, please specify a valid user")
return
#Get the user info
userinfo = await self.webapi.get_user(user_id)
number_of_followers = userinfo.user.num_followers
number_of_friends = userinfo.user.num_friends
number_of_folowing = userinfo.user.num_following
joined_at = (userinfo.user.joined_at).strftime("%d/%m/%Y %H:%M:%S")
try:
last_login = (userinfo.user.last_online_in).strftime("%d/%m/%Y %H:%M:%S")
except:
last_login = "Last login not available"
#Get the number of posts and the most liked post
userposts = await self.webapi.get_posts(author_id = user_id)
number_of_posts = 0
most_likes_post = 0
try:
while userposts.last_id != "":
for post in userposts.posts:
if post.num_likes > most_likes_post:
most_likes_post = post.num_likes
number_of_posts += 1
userposts = await self.webapi.get_posts(author_id = user_id, starts_after=userposts.last_id)
except Exception as e:
print (e)
#Send the info to the chat
await self.highrise.chat(f"""User: {username}\\nNumber of followers: {number_of_followers}\\nNumber of friends: {number_of_friends}\\nNumber of following: {number_of_folowing}\\nJoined at: {joined_at}\\nLast login: {last_login}\\nNumber of posts: {number_of_posts}\\nMost likes in a post: {most_likes_post}""")