Last active
September 29, 2017 08:54
-
-
Save moozer/f2b2bed2ef3b12804d5fef472e5f2713 to your computer and use it in GitHub Desktop.
script for posting messages on matrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# matrix-post-msg | |
# minimal csript to post stuff on matrix | |
# | |
# Uses a yml config file: | |
# user: someuser | |
# password: somepass | |
# server: https://matrix.org | |
# | |
# To send a message | |
# matrix-post-msg.py config.yml '!<some random chars>:matrix.org' 'message message message' | |
# | |
# Note the ' (as opposed to "), to avoid the shell parsing it | |
# | |
import yaml | |
import sys | |
from matrix_client.client import MatrixClient | |
from matrix_client.errors import MatrixRequestError, MatrixUnexpectedResponse | |
defaultConfigFilename = 'config.yaml' | |
def readConfig( filename ): | |
with open( filename, "r") as f: | |
config = yaml.load(f) | |
return config | |
def post_msg(message, room, config): | |
client = MatrixClient(config['server']) | |
try: | |
token = client.login_with_password(username=config['user'], password=config['password']) | |
except MatrixRequestError as e: | |
print(e) | |
if e.code == 403: | |
print("Bad username or password.") | |
sys.exit(4) | |
else: | |
print("Check your sever details are correct.") | |
sys.exit(2) | |
try: | |
room = client.join_room(room) | |
except MatrixRequestError as e: | |
print(e) | |
if e.code == 400: | |
print("Room ID/Alias in the wrong format") | |
sys.exit(11) | |
else: | |
print("Couldn't find room.") | |
sys.exit(12) | |
room.send_text(message) | |
if __name__ == "__main__": | |
if not len(sys.argv) > 3: | |
print( "usage %s <config file> <room id> <message>"%sys.argv[0]) | |
sys.exit(1) | |
# get vars | |
filename = sys.argv[1] | |
room = sys.argv[2] | |
message = ' '.join( sys.argv[3:] ) | |
config = readConfig( filename ) | |
post_msg(message, room, config) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment