77 "fmt"
88 "io"
99 "net/http"
10- "os"
11- "strings"
12- "time"
1310
1411 "github.com/didip/tollbooth/v8"
1512 "github.com/gorilla/mux"
@@ -68,30 +65,6 @@ func (h *HTTPSrv) getMessage(r *http.Request) (string, error) {
6865 return "`Error: no body found. To use a webhook URL, supply a 'msg' URL parameter, or a JSON POST body with a field 'msg'`" , nil
6966}
7067
71- func (h * HTTPSrv ) safeWriteToFile (hookName , content string ) (string , error ) {
72- pattern := fmt .Sprintf ("webhookbot-%s-*.txt" , hookName )
73- file , err := os .CreateTemp ("" , pattern )
74- if err != nil {
75- return "" , fmt .Errorf ("failed to create temp file: %w" , err )
76- }
77- defer func () {
78- if cerr := file .Close (); cerr != nil {
79- h .Errorf ("safeWriteToFile: failed to close temp file: %s" , cerr )
80- }
81- }()
82-
83- if _ , err := file .Write ([]byte (content )); err != nil {
84- return "" , fmt .Errorf ("failed to write file: %w" , err )
85- }
86-
87- // Explicitly sync to ensure all data is written to disk before upload
88- if err := file .Sync (); err != nil {
89- return "" , fmt .Errorf ("failed to sync file: %w" , err )
90- }
91-
92- return file .Name (), nil
93- }
94-
9568func (h * HTTPSrv ) handleHook (w http.ResponseWriter , r * http.Request ) {
9669 vars := mux .Vars (r )
9770 id := vars ["id" ]
@@ -114,37 +87,8 @@ func (h *HTTPSrv) handleHook(w http.ResponseWriter, r *http.Request) {
11487 return
11588 }
11689 h .Stats .Count ("handle - success" )
117- if _ , err := h .Config ().KBC .SendMessageByConvID (hook .ConvID , "[hook: *%s*]\n \n %s" , hook .Name , msg ); err != nil {
118- if base .IsDeletedConvError (err ) {
119- h .Debug ("handleHook: failed to send message: %s" , err )
120- return
121- }
122-
123- // error created in https://github.com/keybase/client/blob/7d6aa64f3fba66adba7a5dd1cc7c523d5086a548/go/chat/msgchecker/plaintext_checker.go#L50
124- if strings .Contains (err .Error (), "exceeds the maximum length" ) {
125- filePath , err := h .safeWriteToFile (hook .Name , msg )
126- if err != nil {
127- h .Errorf ("failed to write attachment file: %s" , err )
128- return
129- }
130- base .GoWithRecover (h .DebugOutput , func () {
131- defer func () {
132- // Cleanup after the file is sent.
133- time .Sleep (time .Minute )
134- h .Debug ("cleaning up %s" , filePath )
135- if err = os .Remove (filePath ); err != nil {
136- h .Errorf ("unable to clean up %s: %v" , filePath , err )
137- }
138- }()
139- title := fmt .Sprintf ("[hook: *%s*]" , hook .Name )
140- if _ , err := h .Config ().KBC .SendAttachmentByConvID (hook .ConvID , filePath , title ); err != nil {
141- h .Errorf ("failed to send attachment %s: %s" , filePath , err )
142- return
143- }
144- })
145- return
146- }
147-
90+ title := fmt .Sprintf ("[hook: *%s*]" , hook .Name )
91+ if err := h .SendMessageByConvIDOrFile (hook .ConvID , title , "%s\n \n %s" , title , msg ); err != nil {
14892 h .Debug ("handleHook: failed to send message: %s" , err )
14993 }
15094}
0 commit comments