Matrix LINE-Bridge: Matrix photoforwarding and location tracking aren’t working. Here are the bug fixes you need.
The problem: I run my own Matrix server (Synapse in Docker) with a LINE bridge. Text messages, audio and video can be forwarded from Element to LINE, but photos can’t. Instead, the bridge bot posts this message: “Your message may not have been bridged: panic in event handler”.
Video: Matrix Photoforwarding and Location BUGFIX
Language: 🇩🇪|🇬🇧
☝️ Use YouTube subtitles for all languages.
Tracking it down
The bridge log /matrix/line-bridge/data/logs/bridge.log shows the same error for every failed photo:
runtime error: invalid memory address or nil pointer dereference
connector.(*LineClient).HandleMatrixMessage → send_message.go:189
At that point the code accesses msg.Content.Info.MimeType, but Info is empty (nil). Two findings followed from that:
- The root cause is in the opposite direction. The handler for incoming LINE photos (
/matrix/line-bridge/matrix-line-messenger/pkg/connector/handlers/image.go) creates anm.imageevent without aninfoblock, so there is no MIME type, size, width or height. Audio and video do get this block, which is why they work. - Forwarding copies the content 1:1. Element takes the event over unchanged. When it is sent back to LINE,
infois missing and the bridge crashes.
Docker’s time zone plays no role here. The logs run in UTC, and 02:20 UTC corresponds to 9:20 in Bangkok.
The fix
Two files need to be changed:
/matrix/line-bridge/matrix-line-messenger/pkg/connector/send_message.go/matrix/line-bridge/matrix-line-messenger/pkg/connector/handlers/image.go
Step 0: Backup
cd /matrix/line-bridge
mkdir -p backup-vor-image-info-fix/handlers
cp matrix-line-messenger/pkg/connector/send_message.go backup-vor-image-info-fix/
cp matrix-line-messenger/pkg/connector/handlers/image.go backup-vor-image-info-fix/handlers/
File 1: /matrix/line-bridge/matrix-line-messenger/pkg/connector/send_message.go
1a) Add an import in /matrix/line-bridge/matrix-line-messenger/pkg/connector/send_message.go (between "image/jpeg" and "regexp"):
"net/http"
1b) The crash site in /matrix/line-bridge/matrix-line-messenger/pkg/connector/send_message.go (around line 189, under case event.MsgImage:). Replace
mimeType := msg.Content.Info.MimeType
with
mimeType := ""
if msg.Content.Info != nil {
mimeType = msg.Content.Info.MimeType
}
if mimeType == "" {
mimeType = http.DetectContentType(data)
}
1c) Optional: harden the video branch in /matrix/line-bridge/matrix-line-messenger/pkg/connector/send_message.go (around line 401, under case event.MsgVideo:). Replace
if msg.Content.Info.Duration > 0 {
with
if msg.Content.Info != nil && msg.Content.Info.Duration > 0 {
The line with Info != nil in the audio branch (around line 546) stays unchanged.
File 2: /matrix/line-bridge/matrix-line-messenger/pkg/connector/handlers/image.go
2a) Replace the import block in /matrix/line-bridge/matrix-line-messenger/pkg/connector/handlers/image.go with:
import (
"bytes"
"context"
"encoding/json"
"fmt"
"image"
_ "image/jpeg"
"time"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/event"
"github.com/highesttt/matrix-line-messenger/pkg/line"
)
2b) Create the info block in /matrix/line-bridge/matrix-line-messenger/pkg/connector/handlers/image.go. At the end of ConvertImage, directly before the last return &bridgev2.ConvertedMessage{, insert:
imgInfo := &event.FileInfo{
MimeType: "image/jpeg",
Size: len(imgData),
}
if cfg, _, cfgErr := image.DecodeConfig(bytes.NewReader(imgData)); cfgErr == nil {
imgInfo.Width = cfg.Width
imgInfo.Height = cfg.Height
}
2c) Put the info into the event in /matrix/line-bridge/matrix-line-messenger/pkg/connector/handlers/image.go. In the same return, add the line Info: imgInfo,:
MsgType: event.MsgImage,
Body: "image.jpg",
URL: mxc,
File: file,
Info: imgInfo,
RelatesTo: relatesTo,
Rebuild and restart
cd /matrix
docker compose build line-bridge
docker compose up -d line-bridge
Testing
Forward an old LINE photo. Change 1b handles that case. Then forward a newly received photo. The log must show no more hits:
docker logs matrix-line-bridge 2>&1 | grep -i "panic\|nil pointer"
Rolling back
cd /matrix/line-bridge
cp backup-vor-image-info-fix/send_message.go matrix-line-messenger/pkg/connector/send_message.go
cp backup-vor-image-info-fix/handlers/image.go matrix-line-messenger/pkg/connector/handlers/image.go
cd /matrix && docker compose build line-bridge && docker compose up -d line-bridge
Conclusion
“Panic in event handler” sounds like an infrastructure problem, but here it was a missing nil check plus an incomplete event. The stack trace line in the log leads straight to the line of code, so it’s worth looking there first whenever a bridge misbehaves.
Sharing Location Between Element and LINE (Matrix Bridge)
The problem: A location shared in LINE shows up in Element only as “[Location unavailable]”. A location shared in Element (“Share my location”) never arrives in LINE.
The causes:
- LINE sends the location encrypted and writes the coordinates as text (
"12.96…"). The bridge expected numbers. - The bridge had no code for sending locations and rejected them with “m.location not implemented”.
All paths start at /matrix. I call the shared folder <BRIDGE>:
<BRIDGE> = /matrix/line-bridge/matrix-line-messenger/pkg/connector
Three files need to be changed. Open them in a text editor and make sure you keep the indentation (tabs).
File 1: /matrix/line-bridge/matrix-line-messenger/pkg/connector/handle_message.go
Find (around line 435):
return h.ConvertLocation(data, replyRelatesTo)
Replace with:
return h.ConvertLocation(data, decryptedBody, replyRelatesTo)
File 2: /matrix/line-bridge/matrix-line-messenger/pkg/connector/handlers/location.go
Step 1: At the top, replace the start of the import block
import (
"fmt"
"strings"
with:
import (
"encoding/json"
"fmt"
"strconv"
"strings"
Step 2: Find these lines:
// ConvertLocation converts a LINE location message to a Matrix location message.
func (h *Handler) ConvertLocation(data line.Message, relatesTo *event.RelatesTo) (*bridgev2.ConvertedMessage, error) {
if data.Location == nil {
Replace them with this block:
// flexFloat accepts JSON numbers and strings ("12.96"); LINE sends coordinates
// as strings inside the decrypted E2EE payload.
type flexFloat float64
func (f *flexFloat) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), `"`)
if s == "" || s == "null" {
return nil
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
*f = flexFloat(v)
return nil
}
// extractLocation reads the location from the plain field or the decrypted payload.
func extractLocation(data line.Message, decryptedBody string) *line.MessageLocation {
if data.Location != nil {
return data.Location
}
body := strings.TrimSpace(decryptedBody)
if !strings.HasPrefix(body, "{") {
return nil
}
var w struct {
Location *struct {
Title string `json:"title"`
Address string `json:"address"`
Latitude flexFloat `json:"latitude"`
Longitude flexFloat `json:"longitude"`
} `json:"location"`
}
if json.Unmarshal([]byte(body), &w) != nil || w.Location == nil {
return nil
}
l := w.Location
return &line.MessageLocation{Title: l.Title, Address: l.Address,
Latitude: float64(l.Latitude), Longitude: float64(l.Longitude)}
}
// ConvertLocation converts a LINE location message to a Matrix location message.
func (h *Handler) ConvertLocation(data line.Message, decryptedBody string, relatesTo *event.RelatesTo) (*bridgev2.ConvertedMessage, error) {
loc := extractLocation(data, decryptedBody)
if loc == nil {
h.Log.Warn().Int("content_type", data.ContentType).Msg("LINE location without usable coordinates")
Step 3: Replace every occurrence of data.Location. in this file with loc. (this affects Latitude, Longitude, Title and Address, about 6 places). The lines data.Location != nil and return data.Location from Step 2 stay unchanged.
File 3: /matrix/line-bridge/matrix-line-messenger/pkg/connector/send_message.go
Step 1: Find
if useE2EEMedia && effectiveMsgType != event.MsgText {
Replace with:
if useE2EEMedia && effectiveMsgType != event.MsgText && effectiveMsgType != event.MsgLocation {
Step 2: Find the line var plainTextBody string … and add directly below it:
var locationData *line.MessageLocation // set for m.location messages
Step 3: At the end of the switch effectiveMsgType {, find the line default: (below it is message type %s not implemented). Add the following directly above it:
case event.MsgLocation:
lat, lon, ok := parseGeoURI(msg.Content.GeoURI)
if !ok {
return nil, fmt.Errorf("invalid geo URI in m.location: %q", msg.Content.GeoURI)
}
address := "Location"
first := strings.TrimSpace(strings.SplitN(msg.Content.Body, "\n", 2)[0])
if first != "" && len(first) <= 100 && !strings.Contains(first, "geo:") {
address = first
}
locationData = &line.MessageLocation{Title: address, Address: address, Latitude: lat, Longitude: lon}
contentType = int(ContentLocation)
if !plainText {
// Same shape LINE uses for received locations: coordinates as strings.
payload, err = json.Marshal(map[string]any{"location": map[string]string{
"address": address,
"latitude": strconv.FormatFloat(lat, 'f', -1, 64),
"longitude": strconv.FormatFloat(lon, 'f', -1, 64),
}})
if err != nil {
return nil, fmt.Errorf("failed to marshal location payload: %w", err)
}
}
Step 4: Find
HasContent: contentType != int(ContentText),
Replace with:
HasContent: contentType != int(ContentText) && contentType != int(ContentLocation),
Step 5: Find
if plainText {
lineMsg.Text = plainTextBody
} else {
Replace with:
if plainText {
lineMsg.Text = plainTextBody
if locationData != nil {
lineMsg.Location = locationData
}
} else {
Step 6: Find the function contentTypeForMsgType and add these two lines directly below switch msgType {:
case event.MsgLocation:
return int(ContentLocation)
Step 7: Add this new function at the very end of the file:
// parseGeoURI parses "geo:13.7563,100.5018;u=20" into latitude and longitude.
func parseGeoURI(uri string) (lat, lon float64, ok bool) {
s := strings.TrimPrefix(strings.TrimSpace(uri), "geo:")
s = strings.SplitN(s, ";", 2)[0]
parts := strings.Split(s, ",")
if len(parts) < 2 {
return 0, 0, false
}
var err1, err2 error
lat, err1 = strconv.ParseFloat(strings.TrimSpace(parts[0]), 64)
lon, err2 = strconv.ParseFloat(strings.TrimSpace(parts[1]), 64)
return lat, lon, err1 == nil && err2 == nil
}
Rebuild and test
- In the terminal:
cd /matrix docker compose build line-bridge && docker compose up -d line-bridge- Test 1: Send a location in LINE. Element now shows a map with the address.
- Test 2: In Element, choose “Share my location”. The location arrives in LINE.
I tested both directions this way, and they work.
Note on live location: Element’s “Share live location” is not covered by this change. Element uses different events for it, which the bridge does not forward. Static locations work in both directions.

Support / Donation Link for the Channel
If my posts have been helpful or supported you in any way, I’d truly appreciate your support 🙏
#Matrix #Selfhosting #Docker #LINE #Synapse