From db21b023420fa25e7517e1069d1e99f3877fd6e2 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 17 Dec 2023 18:15:31 -0800 Subject: [PATCH] Add media links to messages from pnut.io Updated to woodstock v0.4.0 and added links to media from pnut.io messages. --- go.mod | 2 +- go.sum | 2 ++ pnut-bridge.go | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 82e2822..623b17b 100644 --- a/go.mod +++ b/go.mod @@ -8,5 +8,5 @@ require ( github.com/mitchellh/go-wordwrap v1.0.1 github.com/smartystreets/goconvey v1.6.4 // indirect gopkg.in/ini.v1 v1.62.0 - mcmillian.dev/go/woodstock v0.3.1 + mcmillian.dev/go/woodstock v0.4.0 ) diff --git a/go.sum b/go.sum index f1edfcf..38c20fa 100644 --- a/go.sum +++ b/go.sum @@ -28,3 +28,5 @@ gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= mcmillian.dev/go/woodstock v0.3.1 h1:m88zZQ726dw+jBpPXhqbdeTXHL5/rnJ4g1MOzV+oMm0= mcmillian.dev/go/woodstock v0.3.1/go.mod h1:1CUHYZ1VUVX22aCcG9Pc/W9ZnwtFzHoyN4YHaAguO/I= +mcmillian.dev/go/woodstock v0.4.0 h1:3ROqcfnfuihKMcyQ3TsaP4Z1vACEjEyvwtBM2HYfuhc= +mcmillian.dev/go/woodstock v0.4.0/go.mod h1:1CUHYZ1VUVX22aCcG9Pc/W9ZnwtFzHoyN4YHaAguO/I= diff --git a/pnut-bridge.go b/pnut-bridge.go index e8c86e5..437a5d8 100644 --- a/pnut-bridge.go +++ b/pnut-bridge.go @@ -181,6 +181,42 @@ func pnutMsgHandler(conf *Config, msg woodstock.Message) { for _, v := range msg.Content.Entities.Links { linktext = "| " + linktext + v.URL + "\n" } + for rawtype, value := range msg.Raw { + if rawtype == "io.pnut.core.oembed" { + for _, v := range value { + var embed woodstock.RawEmbed + byteData, _ := json.Marshal(v) + jerr := json.Unmarshal(byteData, &embed) + if jerr != nil { + log.Println("Error unmarshaling raw", jerr) + } + switch embed.Type { + case "photo": + var photo woodstock.PhotoEmbed + jerr := json.Unmarshal(byteData, &photo) + if jerr != nil { + log.Println("Error unmarshaling photo", jerr) + } + linktext = linktext + "🖼️\n" + photo.URL + "\n" + case "video": + var video woodstock.VideoEmbed + jerr := json.Unmarshal(byteData, &video) + if jerr != nil { + log.Println("Error unmarshaling video", jerr) + } + linktext = linktext + "📽️\n" + video.URL + "\n" + case "audio": + var audio woodstock.AudioEmbed + jerr := json.Unmarshal(byteData, &audio) + if jerr != nil { + log.Println("Error unmarshaling audio", jerr) + } + linktext = linktext + "🔈\n" + audio.URL + "\n" + default: + } + } + } + } if len(linktext) > 0 { msgtext = msgtext + "\n\n" + linktext } @@ -262,7 +298,7 @@ func bridgeMsgHandler(conf *Config, msg MbMessage) { line := msg.Username + " " + line log.Printf(">> sending message from %s to channel %s ...", msg.Gateway, channelID) newmessage := woodstock.NewMessage{Text: line, Raw: raw} - _, err := client.CreateMessage(channelID, newmessage) + _, err := client.CreateMessage(channelID, newmessage, url.Values{}) if err != nil { log.Println("!! problem posting message to pnut.io", err) } @@ -316,6 +352,7 @@ func main() { params := url.Values{} params.Set("access_token", conf.PnutAccessToken) + params.Set("include_raw", "1") socketURL := "wss://stream.pnut.io/v1/user?" + params.Encode() conn, _, err := websocket.DefaultDialer.Dial(socketURL, nil) -- 2.45.2