shawnanastasio / python-matrix-bot-api

A Python API for making Matrix bots (https://matrix.org).
GNU General Public License v3.0
90 stars 34 forks source link

Invalid JSON request to matrix.org #16

Closed deadvcr closed 5 years ago

deadvcr commented 5 years ago
import random
import requests
import aiohttp

from matrix_bot_api.matrix_bot_api import MatrixBotAPI
from matrix_bot_api.mregex_handler import MRegexHandler
from matrix_bot_api.mcommand_handler import MCommandHandler

USERNAME = "{username}"
PASSWORD = "{password}"
SERVER   = "https://matrix.org/_matrix/client/r0/login"

def godsays(room, event):
    with aiohttp.ClientSession() as session:
        with session.get("http://templeos.net/") as r:
            if r.status == 200:
                rbody = r.text()
                return rbody
            else:
                return None

def main():
    bot = MatrixBotAPI(USERNAME, PASSWORD, SERVER)

    god_handler = MRegexHandler("!god", godsays)
    bot.add_handler(god_handler)

    bot.start_polling()

    while True:
        input()

if __name__ == "__main__":
    main()

When run, it throws an error saying the following:

400: {"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}
Check your server details are correct.

The server details are definitely correct, because I can use curl with the same information and get a response from the server, like so:

 deadvcr@lambda-core:~$ curl -XPOST -d '{"type":"m.login.password", "user":"{username}", "password":"{password}"}' "https://matrix.org/_matrix/client/r0/login"
{
    "access_token": "{access token}",
    "device_id": "{device id}",
    "home_server": "matrix.org",
    "user_id": "@{username}:matrix.org"
}
shawnanastasio commented 5 years ago

For the SERVER variable, you should simply use the top level domain and protocol, not the full path to the login endpoint.

In your case, it should probably read SERVER = "https://matrix.org"

deadvcr commented 5 years ago

Ah, that makes sense. Thank you for the help, keep up the great work!