Roblox API does not behave as expected when called with Python Request module

I am trying to make a Python program to track users status (online-offline-ingame).
The program uses the Request module for Python to request user’s status and saves it into an Excel file every 2 minutes, where I can review the data.
However, I also want to add the function to see which game a user is in, but API output does not include the lastLocation object.

Currently, example graph generated looks like this:


Graph spacing is a little off because of size constraint.
Key 0-Offline, 1-Online, 2-Ingame

API output from the program is:

{"userPresences":[{"userPresenceType":2,"lastLocation":"","placeId":null,"rootPlaceId":null,"gameId":null,"universeId":null,"userId":##########}]}

API output from documentation with same user ID is:

{
  "userPresences": [
    {
      "userPresenceType": 2,
      "lastLocation": "VR Hands v3.1 🎙️",
      "placeId": 4832438542,
      "rootPlaceId": 4832438542,
      "gameId": "325a0256-2a3d-44ed-a139-22f484adf871",
      "universeId": 1638323641,
      "userId": ##########
    }
  ]
}

Program code is: (in Python 3.9)

import time
import requests
import json
from datetime import datetime
from openpyxl import Workbook, load_workbook
import os


def check_status(user_id):
    """ Checks status of player using
    Roblox API endpoint Presence V1"""

    payload = {'userIds': [user_id]}
    presence = requests.post("https://presence.roblox.com/v1/presence/users", json=payload)
    presence_data = json.loads(presence.text)

    print(presence.text)
    if presence is None:
        return "ServerError"

    if presence_data["userPresences"][0]["userPresenceType"] == 1:
        return "Online"
    elif presence_data["userPresences"][0]["userPresenceType"] in (2, 3):
        if presence_data["userPresences"][0]["userPresenceType"] == 2:
            return "InGame"
        else:
            return "InStudio"
    elif presence_data["userPresences"][0]["userPresenceType"] == 0:
        return "Offline"


excel_file = "status_log.xlsx"

# Check if file exists, if not create it
print("Enter User ID to track:")
c_user_id = input()

if not os.path.exists(excel_file):
    wb = Workbook()
    ws = wb.active
    ws.append(["Timestamp", "Status", "Status Code"])  # Add header
    wb.save(excel_file)

while True:
    # Get current timestamp and status
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    status = check_status(c_user_id)
    status_code = 5

    # Get current status code from previous
    if status == "Offline":
        status_code = 0
    elif status == "Online":
        status_code = 1
    elif "InGame" in status:
        status_code = 2
    elif status == "InStudio":
        status_code = 3

    if status is None:
        status_code = 5

    # Put new row into file
    wb = load_workbook(excel_file)
    ws = wb.active
    ws.append([timestamp, status, status_code])
    wb.save(excel_file)

    print("["+timestamp+"]"+" Status recorded as "+status+" Code "+str(status_code))

    time.sleep(120)


I am wondering if I should use a Roblox API wrapper such as Ro.py to avoid problems, but I think it is not necessary for a simple project such as this.

Thank you for your advice and support.

1 Like

I believe this a user privacy thing, you can’t see what game they’re playing unless they have set it to be visible to you.

It could be, but if so, then the documentation simulation thingamabob would have returned the same values as my program.

Heres the link you can try it yourself: Swagger UI

Can you provide the link to that documentation?

Sure! It’s from someone’s GitHub where he documents Roblox Web APIs and there’s a simulator so you can input example values. I edited the reply above.

I believe that simulator (it’s actually sending the requests, so not a simulator!) uses your account’s token, so if you’re testing it on someone you personally can see the presence of, it will return results, otherwise it’s just null.

Also that’s not from “someone’s” GitHub, that’s the old official documentation for roblox’s web APIs.

Sorry, I got the link for the documentation on a GitHub link, so I assumed he made it himself, since the new official Roblox documentation doesn’t have a request sender.

So, if I want to have my program record the games people are in I will have to authenticate it with my account token?

I would strongly suggest not doing that, even if you did, users would only be able to see the user presences you can see (your friends).

It’s just how it is, privacy.

Alright, I guess that means the only way is to manually check some of the values.
Thank you for the support!

Your account token resets every time you log out and log back in. You would have to update it every day or something like that. You could probably automate it somehow by looking through your browser’s cookies, but that’s pretty suspicious activity that your antivirus would most likely flag. I believe stored cookies are also encrypted. It’d just be all around complicated, but if you’re going to manually check anyways, you’re only going to see the values you can see if you do it automatically with your token.

You can simply check if you can view the game by verifying whether or not lastLocation is an empty string. If you can’t, you handle it and say N/A or something. If you can, you execute the function.
The only thing you really need for that graph is userPresenceType, which you can view either way. (0 = offline, 1 = website, 2 = ingame)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.