mautrix/go

Send formatted message

Oyabi opened this issue · 2 comments

Oyabi commented

Hi,

How can I send formatted message in markdown or html to a chatroom ?
Here you can find my simple try with the example code :

package main

import (
	"flag"
	"fmt"
	"os"
	"time"

	"maunium.net/go/mautrix"
	"maunium.net/go/mautrix/event"
)

var homeserver = flag.String("homeserver", "", "Matrix homeserver")
var username = flag.String("username", "", "Matrix username localpart")
var password = flag.String("password", "", "Matrix password")

func main() {
	flag.Parse()
	if *username == "" || *password == "" || *homeserver == "" {
		_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(1)
	}

	fmt.Println("Logging into", *homeserver, "as", *username)
	client, err := mautrix.NewClient(*homeserver, "", "")
	if err != nil {
		panic(err)
	}
	_, err = client.Login(&mautrix.ReqLogin{
		Type:             "m.login.password",
		Identifier:       mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *username},
		Password:         *password,
		StoreCredentials: true,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("Login successful")

	syncer := client.Syncer.(*mautrix.DefaultSyncer)
	syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
		diff := time.Since(time.Unix(evt.Timestamp/1000, 0))
		if diff < time.Second*10 {
			if evt.Sender != client.UserID {
				fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
				client.SendText(evt.RoomID, "`nok`")
				client.SendText(evt.RoomID, "```html\n<b>nok</b>\n```")
				client.SendText(evt.RoomID, "<code>nok</code>")
			}
		}
	})

	err = client.Sync()
	if err != nil {
		panic(err)
	}
}

None of the messages are formatted in Element:
image

When I inspect a formatted message, the json seems like that:

{
  "type": "m.room.message",
  "sender": "SENDER",
  "content": {
    "msgtype": "m.text",
    "body": "`ok`",
    "format": "org.matrix.custom.html",
    "formatted_body": "<code>ok</code>"
  },
  "origin_server_ts": 1611261145159,
  "unsigned": {
    "age": 73212340,
    "transaction_id": "TRANSACTION_ID"
  },
  "event_id": "EVENT_ID",
  "room_id": "ROOM_ID"
}

The non functionnal message look like that:

{
  "type": "m.room.message",
  "sender": "SENDER",
  "content": {
    "msgtype": "m.text",
    "body": "`nok`"
  },
  "origin_server_ts": 1611340455640,
  "unsigned": {
    "age": 308
  },
  "event_id": "EVENT_ID",
  "room_id": "ROOM_ID"
}

As you can see, format and formatted_body are missing in my message but I can't find a method in client to send html.

Thank you.

tulir commented

Use format.RenderMarkdown to render markdown into a properly structured Matrix message, then use Client.SendMessageEvent() to send that content:

import (
	"maunium.net/go/mautrix"
	"maunium.net/go/mautrix/format"
	"maunium.net/go/mautrix/event"
)

client := mautrix.NewClient(...)
...
content := format.RenderMarkdown("**hello *world***", true, false)
client.SendMessageEvent("!foo:example.com", event.EventMessage, &content)
Oyabi commented

Perfect, thank you very much ;)
Have a nice day.