8go / matrix-eno-bot

An admin and personal assistence Matrix bot built on matrix-nio and nio-template
Apache License 2.0
88 stars 15 forks source link

Run event (send message) via command line #29

Closed balimeister closed 2 years ago

balimeister commented 2 years ago

Is there any information on how to trigger an event like (send_text_to_room) via an outside script. I like to run a cronjob/bashscript that sends a message to a room. Maybe someone already has something like this and can share it.

unreal79 commented 2 years ago

My Python script to send a message to Matrix room:

matrix_jsonload = json.dumps( {"type":"m.login.password", "user":matrix_user, "password":matrix_password} )
matrix_response = requests.post( "https://matrix.org/_matrix/client/r0/login", data = matrix_jsonload )
matrix_token = json.loads( matrix_response.text )['access_token']
matrix_jsonload = json.dumps( {"msgtype": "m.text", "body": some_string } )
matrix_response = requests.post( "https://matrix.org/_matrix/client/r0/rooms/" + matrix_room + "/send/m.room.message?access_token=" + matrix_token, data = matrix_jsonload )
balimeister commented 2 years ago

I solved it with this bash command (auth token and room_id required, as the id i used a timestamp): curl -X PUT "http://127.0.0.1:8008/_matrix/client/r0/rooms/${room}/send/m.room.message/${id}" -H "Authorization: Bearer ${token}" -d "{\"msgtype\":\"m.text\",\"body\":\"$message\"}"

8go commented 2 years ago

Alternatively, you can use matrix-commander, a simple Python CLI program, that does just that. Check out the repo https://github.com/8go/matrix-commander

And thank you @balimeister for providing a quick curl based solution to your own question.