Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Matrix Webhook
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Guilhem Saurel
Matrix Webhook
Commits
496108dc
Commit
496108dc
authored
6 years ago
by
Guilhem Saurel
Browse files
Options
Downloads
Patches
Plain Diff
add a matrix client to the server
parent
781e4fd7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+28
-3
28 additions, 3 deletions
main.py
with
28 additions
and
3 deletions
main.py
+
28
−
3
View file @
496108dc
...
...
@@ -2,14 +2,39 @@
"""
wifi-with-matrix script.
Bridge between https://code.ffdn.org/FFDN/wifi-with-me & a matrix room
Needs the following environment variables:
- MMW_BOT_MATRIX_URL: the url of the matrix homeserver
- MMW_BOT_MATRIX_ID: the user id of the bot on this server
- MMW_BOT_MATRIX_PW: the password for this user
- MMW_BOT_ROOM_ID: the room on which send the notifications
"""
import
os
from
http.server
import
BaseHTTPRequestHandler
,
HTTPServer
SERVER_ADDRESS
=
(
''
,
4785
)
from
matrix_client.client
import
MatrixClient
SERVER_ADDRESS
=
(
''
,
int
(
os
.
environ
.
get
(
'
MMW_BOT_PORT
'
,
4785
)))
MATRIX_URL
=
os
.
environ
.
get
(
'
MMW_BOT_MATRIX_URL
'
,
'
https://matrix.org
'
)
MATRIX_ID
=
os
.
environ
.
get
(
'
MMW_BOT_MATRIX_ID
'
,
'
wwm
'
)
MATRIX_PW
=
os
.
environ
[
'
MMW_BOT_MATRIX_PW
'
]
ROOM_ID
=
os
.
environ
[
'
MMW_BOT_ROOM_ID
'
]
class
WWMBotServer
(
HTTPServer
):
"""
an HTTPServer that also contain a matrix client
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
matrix_client
=
MatrixClient
(
MATRIX_URL
)
self
.
matrix_token
=
self
.
matrix_client
.
login
(
username
=
MATRIX_ID
,
password
=
MATRIX_PW
)
self
.
matrix_room
=
self
.
matrix_client
.
get_rooms
()[
ROOM_ID
]
class
Forwarder
(
BaseHTTPRequestHandler
):
class
WWMBot
Forwarder
(
BaseHTTPRequestHandler
):
"""
Class given to the server, st. it knows what to do with a request.
This one handles the HTTP request, and forwards it to the matrix room.
...
...
@@ -32,4 +57,4 @@ class Forwarder(BaseHTTPRequestHandler):
if
__name__
==
'
__main__
'
:
HTTP
Server
(
SERVER_ADDRESS
,
Forwarder
).
serve_forever
()
WWMBot
Server
(
SERVER_ADDRESS
,
WWMBot
Forwarder
).
serve_forever
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment