-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added send message test
- Loading branch information
Showing
6 changed files
with
102 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import axios from "axios"; | ||
const server = 'https://zeniq.chat/'; | ||
|
||
/** | ||
* TODO: document here | ||
*/ | ||
export async function nomoCreateRoom(args: Map<string, any>, accessToken: string): Promise<string> { | ||
const url = server + `_matrix/client/v3/createRoom`; | ||
|
||
try { | ||
const response = await axios.post(url, args, { | ||
headers: { | ||
'Authorization': `Bearer ${accessToken}`, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
return response.data['room_id']; | ||
} catch (error) { | ||
console.error('Error sending message:', error); | ||
return ""; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,29 @@ | ||
|
||
import axios from "axios"; | ||
const server = 'https://zeniq.chat/'; | ||
export interface SendMessageArgs { | ||
room: string; | ||
content: string; | ||
roomId: string; | ||
content: Map<string, any>; | ||
transationID: string; | ||
eventType: string; | ||
} | ||
|
||
/** | ||
* TODO: document here | ||
*/ | ||
export async function nomoSendMessage(args: SendMessageArgs) { | ||
console.log("nomoSendMessage not implemented"); | ||
export async function nomoSendMessage(args: SendMessageArgs, accessToken: string): Promise<string> { | ||
let { roomId, content, transationID, eventType } = args; | ||
const url = server + `_matrix/client/v3/rooms/${roomId}/send/${eventType}/${transationID}`; | ||
|
||
try { | ||
const response = await axios.put(url, content, { | ||
headers: { | ||
'Authorization': `Bearer ${accessToken}`, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
return response.data['event_id']; | ||
} catch (error) { | ||
console.error('Error sending message:', error); | ||
return ""; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { nomoRegisterOrLogin, UserMatrix, } from "../../src"; | ||
import { nomoCreateRoom } from "../../src/create_room/create_room"; | ||
import { generateRandomString } from "../test_util"; | ||
|
||
test("create room", async () => { | ||
const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe"; | ||
const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic); | ||
const args: Map<string, any> = new Map | ||
{ | ||
[ | ||
['name', generateRandomString(25)], | ||
["preset", "private_chat",], | ||
['room_alias_name', generateRandomString(10)] | ||
] | ||
}; | ||
let roomId = await nomoCreateRoom(args, userMatrix.access_token); | ||
console.log(roomId); | ||
if (roomId.length == 0) { | ||
fail("no roomId"); | ||
} | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,38 @@ | ||
import {nomoSendMessage} from "../../src"; | ||
import { nomoSendMessage, nomoRegisterOrLogin, UserMatrix, SendMessageArgs } from "../../src"; | ||
import { nomoCreateRoom } from "../../src/create_room/create_room"; | ||
import { generateRandomString } from "../test_util"; | ||
|
||
test("send message", async () => { | ||
await nomoSendMessage({content: "foo", room: "bar"}); | ||
const mnemonic = "diet say develop title sibling steel blast table chicken foster fuel giraffe"; | ||
const userMatrix: UserMatrix = await nomoRegisterOrLogin(mnemonic); | ||
const args: Map<string, any> = new Map | ||
{ | ||
[ | ||
['name', generateRandomString(25)], | ||
["preset", "private_chat",], | ||
['room_alias_name', generateRandomString(10)] | ||
] | ||
}; | ||
let roomId = await nomoCreateRoom(args, userMatrix.access_token); | ||
if (roomId.length == 0) { | ||
fail("no roomId"); | ||
} | ||
const contentMap: Map<string, any> = new Map | ||
{ | ||
[ | ||
['body', generateRandomString(25)], | ||
["msgtype", "m.text"], | ||
] | ||
}; | ||
const testMessage: SendMessageArgs = | ||
{ | ||
content: contentMap, | ||
roomId: roomId, | ||
transationID: generateRandomString(200), | ||
eventType: "m.room.message", | ||
}; | ||
let eventId = await nomoSendMessage(testMessage, userMatrix.access_token); | ||
if (eventId.length == 0) { | ||
fail("eventId empty"); | ||
} | ||
}); |
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