improve message sending
This commit is contained in:
parent
f3cde63e37
commit
b6ee0f901e
4 changed files with 29 additions and 13 deletions
|
|
@ -75,14 +75,10 @@ func (mxc *MXClient) _onMessage(ctx context.Context, evt *event.Event) {
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
type sendmessage_data_content struct {
|
|
||||||
Body string `json:"body"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type sendmessage_data struct {
|
type sendmessage_data struct {
|
||||||
RoomId id.RoomID `json:"roomid"`
|
RoomId id.RoomID `json:"roomid"`
|
||||||
Type event.Type `json:"type"`
|
Type event.Type `json:"type"`
|
||||||
Content sendmessage_data_content `json:"content"`
|
Content map[string]any `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mxc *MXClient) SendRoomMessage(ctx context.Context, data string) (*mautrix.RespSendEvent, error) {
|
func (mxc *MXClient) SendRoomMessage(ctx context.Context, data string) (*mautrix.RespSendEvent, error) {
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,13 @@ class DemoBot(SMALBot):
|
||||||
if msg["content"]["body"].startswith("!echo"):
|
if msg["content"]["body"].startswith("!echo"):
|
||||||
logger.error(f"reply to this: {msg}")
|
logger.error(f"reply to this: {msg}")
|
||||||
|
|
||||||
self.sendmessage(msg["roomid"], "huhu")
|
txt = msg["content"]["body"][5:].strip()
|
||||||
|
|
||||||
|
if txt.strip() == "":
|
||||||
|
txt = "Are you kidding me?"
|
||||||
|
|
||||||
|
# self.sendmessage(msg["roomid"], txt)
|
||||||
|
self.sendmessagereply(msg["roomid"], msg["id"], msg["sender"], txt)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,5 +30,22 @@ class SMALBot(SMALApp):
|
||||||
data["roomid"] = roomid
|
data["roomid"] = roomid
|
||||||
data["content"] = {}
|
data["content"] = {}
|
||||||
data["content"]["body"] = text
|
data["content"]["body"] = text
|
||||||
|
data["content"]["msgtype"] = "m.text"
|
||||||
|
|
||||||
|
self._sendmessage(data)
|
||||||
|
|
||||||
|
def sendmessagereply(self, roomid, msgid, mxid, text):
|
||||||
|
data = {}
|
||||||
|
data["roomid"] = roomid
|
||||||
|
data["content"] = {}
|
||||||
|
data["content"]["body"] = text
|
||||||
|
data["content"]["msgtype"] = "m.text"
|
||||||
|
data["content"]["m.mentions"] = {}
|
||||||
|
data["content"]["m.mentions"]["user_ids"] = [
|
||||||
|
mxid,
|
||||||
|
]
|
||||||
|
data["content"]["m.relates_to"] = {}
|
||||||
|
data["content"]["m.relates_to"]["m.in_reply_to"] = {}
|
||||||
|
data["content"]["m.relates_to"]["m.in_reply_to"]["event_id"] = msgid
|
||||||
|
|
||||||
self._sendmessage(data)
|
self._sendmessage(data)
|
||||||
|
|
|
||||||
|
|
@ -74,11 +74,8 @@ class _MXClient:
|
||||||
def _sendmessage(self, data_dict):
|
def _sendmessage(self, data_dict):
|
||||||
data = json.dumps(data_dict).encode(encoding="utf-8")
|
data = json.dumps(data_dict).encode(encoding="utf-8")
|
||||||
r = lib.apiv0_sendmessage(self.client_id, data)
|
r = lib.apiv0_sendmessage(self.client_id, data)
|
||||||
result = ffi.string(r)
|
result = checkApiError(r)
|
||||||
lib.FreeCString(r)
|
return result
|
||||||
# if result.startswith(b"ERR:"):
|
|
||||||
# raise APIError(result)
|
|
||||||
print("_sendmessage: ", result)
|
|
||||||
|
|
||||||
def process_event(self, evt):
|
def process_event(self, evt):
|
||||||
if hasattr(self, "on_event") and callable(self.on_event):
|
if hasattr(self, "on_event") and callable(self.on_event):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue