Init. Add matrix-ping-pong
This commit is contained in:
commit
e0ddc61e75
8 changed files with 533 additions and 0 deletions
16
matrix-ping-pong/.gitignore
vendored
Normal file
16
matrix-ping-pong/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
.dub
|
||||
docs.json
|
||||
__dummy.html
|
||||
docs/
|
||||
/matrix-ping-pong
|
||||
matrix-ping-pong.so
|
||||
matrix-ping-pong.dylib
|
||||
matrix-ping-pong.dll
|
||||
matrix-ping-pong.a
|
||||
matrix-ping-pong.lib
|
||||
matrix-ping-pong-test-*
|
||||
*.exe
|
||||
*.pdb
|
||||
*.o
|
||||
*.obj
|
||||
*.lst
|
||||
4
matrix-ping-pong/README.md
Normal file
4
matrix-ping-pong/README.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Matrix Ping-Pong
|
||||
|
||||
Simple Matrix chat bot example using [vibe.d](https://vibed.org/)'s [RestInterfaceClient](https://vibed.org/api/vibe.web.rest/)
|
||||
|
||||
14
matrix-ping-pong/dub.json
Normal file
14
matrix-ping-pong/dub.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"authors": [
|
||||
"nazrin"
|
||||
],
|
||||
"copyright": "Copyright © 2024, nazrin",
|
||||
"dependencies": {
|
||||
"vibe-d:data": "~>0.10.0",
|
||||
"vibe-d:web": "~>0.10.0",
|
||||
"vibe-http": "~>1.0.0"
|
||||
},
|
||||
"description": "Matrix ping-pong bot",
|
||||
"license": "MPL-2.0",
|
||||
"name": "matrix-ping-pong"
|
||||
}
|
||||
21
matrix-ping-pong/dub.selections.json
Normal file
21
matrix-ping-pong/dub.selections.json
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"fileVersion": 1,
|
||||
"versions": {
|
||||
"diet-ng": "1.8.1",
|
||||
"eventcore": "0.9.29",
|
||||
"libasync": "0.8.6",
|
||||
"memutils": "1.0.10",
|
||||
"mir-linux-kernel": "1.0.1",
|
||||
"openssl": "3.3.3",
|
||||
"openssl-static": "1.0.3+3.0.8",
|
||||
"stdx-allocator": "2.77.5",
|
||||
"taggedalgebraic": "0.11.22",
|
||||
"vibe-container": "1.3.0",
|
||||
"vibe-core": "2.8.2",
|
||||
"vibe-d": "0.10.0",
|
||||
"vibe-http": "1.0.0",
|
||||
"vibe-inet": "1.0.0",
|
||||
"vibe-serialization": "1.0.2",
|
||||
"vibe-stream": "1.1.0"
|
||||
}
|
||||
}
|
||||
36
matrix-ping-pong/src/main.d
Normal file
36
matrix-ping-pong/src/main.d
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
import std.stdio, std.string;
|
||||
|
||||
import vibe.core.log;
|
||||
import vibe.data.json;
|
||||
|
||||
import matrix;
|
||||
|
||||
void main(string[] argv){
|
||||
const string homeserver = argv[1];
|
||||
const string token = argv[2];
|
||||
MatrixClient client = new MatrixClient(homeserver, token);
|
||||
logInfo("Logged in as " ~ client.id);
|
||||
|
||||
while(true){
|
||||
Json s = client.sync();
|
||||
if("rooms" in s && "join" in s["rooms"]){
|
||||
foreach(rid, room; s["rooms"]["join"].byKeyValue){
|
||||
foreach(ev; room["timeline"]["events"].byValue){
|
||||
switch(ev["type"].get!string){
|
||||
case "m.room.message":
|
||||
const string body = ev["content"]["body"].get!string;
|
||||
const string uid = ev["sender"].get!string;
|
||||
if(body.toLower == "ping")
|
||||
client.sendRoomMessage(uid ~ " pong!", "m.text", rid);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
64
matrix-ping-pong/src/matrix.d
Normal file
64
matrix-ping-pong/src/matrix.d
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
import vibe.web.rest;
|
||||
import vibe.data.json;
|
||||
import vibe.http.client;
|
||||
|
||||
// https://spec.matrix.org/latest/client-server-api/
|
||||
@path("/_matrix/")
|
||||
interface MatrixAPI{
|
||||
import std.uuid : randomUUID;
|
||||
@method(HTTPMethod.GET){
|
||||
@path("/client/v3/sync"){
|
||||
Json sync(string since, bool full_state = false, int timeout = 60_000);
|
||||
Json sync();
|
||||
}
|
||||
@path("/client/v3/profile/:userId"){
|
||||
Json profile(string _userId);
|
||||
}
|
||||
@path("/client/v3/account/whoami"){
|
||||
Json whoami();
|
||||
}
|
||||
}
|
||||
@method(HTTPMethod.PUT){
|
||||
@path("/client/v3/rooms/:roomId/send/:type/:txn"){
|
||||
Json sendRoomMessage(
|
||||
string body, string msgtype, string _roomId, string _type = "m.room.message", string _txn = randomUUID.toString);
|
||||
Json sendRoomMessage(
|
||||
@viaBody() Json body, string _roomId, string _type = "m.room.message", string _txn = randomUUID.toString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MatrixClient : RestInterfaceClient!MatrixAPI{
|
||||
private string _id;
|
||||
private string nextBatch;
|
||||
this(string path, string token){
|
||||
super(path);
|
||||
this.requestFilter = (ref req){
|
||||
req.headers["Authorization"] = "Bearer " ~ token;
|
||||
};
|
||||
/* this.requestBodyFilter = (req, body){ */
|
||||
/* debug writeln(cast(string)body.peek()); */
|
||||
/* }; */
|
||||
}
|
||||
override Json sync(){
|
||||
Json j;
|
||||
if(!nextBatch)
|
||||
j = super.sync();
|
||||
else
|
||||
j = super.sync(nextBatch);
|
||||
nextBatch = j["next_batch"].get!string;
|
||||
return j;
|
||||
}
|
||||
string id(){
|
||||
if(!_id)
|
||||
_id = whoami["user_id"].get!string;
|
||||
return _id;
|
||||
}
|
||||
Json profile(){
|
||||
return super.profile(id);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue