Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add README.md and msgtype config key #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# matrix-send

Send messages to matrix from command line. Usage example:

```
$ ./matrix-send.py "Hello world!"
```

## Configuration

Copy the config.ini.template file to ```~/.config/matrix-send/config.ini``` and fill the fields:

```
[DEFAULT]
endpoint=https://matrix-server:8448/_matrix/
access_token=
channel_id=
msgtype=m.notice
```

### Getting the access_token and channel_id values

Please, refer to matrix' documentation:
https://matrix.org/docs/guides/client-server.html
1 change: 1 addition & 0 deletions config.ini.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
endpoint=https://matrix-server:8448/_matrix/
access_token=
channel_id=
msgtype=m.notice
6 changes: 4 additions & 2 deletions matrix-send.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def url_quote(input: str) -> str:
return urllib.parse.quote_plus(input)

def send_message(endpoint: str, access_token: str, channel_id: str, message: str, timeout: int) -> bool:
def send_message(endpoint: str, access_token: str, channel_id: str, msgtype: str, message: str, timeout: int) -> bool:
message_id = datetime.now().strftime("m%s.%f")
ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLSv1_2)
url = "{endpoint}client/r0/rooms/{channel_id}/send/m.room.message/{message_id}?access_token={access_token}".format(
Expand All @@ -24,7 +24,7 @@ def send_message(endpoint: str, access_token: str, channel_id: str, message: str
message_id=url_quote(message_id),
access_token=url_quote(access_token)
)
body = json.dumps({"msgtype": "m.notice",
body = json.dumps({"msgtype": msgtype,
"format": "org.matrix.custom.html",
"body": message,
"formatted_body": message}).encode()
Expand Down Expand Up @@ -52,6 +52,7 @@ def main(argv: List[str]):
endpoint = default['endpoint'] # type: str
access_token = default['access_token'] # type: str
channel_id = default['channel_id'] # type: str
msgtype = default.get('msgtype', 'm.notice') # type: str
timeout = int(default.get('timeout', "10"))

if args.message is None:
Expand All @@ -63,6 +64,7 @@ def main(argv: List[str]):
if send_message(endpoint=endpoint,
access_token=access_token,
channel_id=channel_id,
msgtype=msgtype,
message=message,
timeout=timeout):
return 0
Expand Down