Merge pull request #145 from velour/slackapp

slack: revert to manual slack call for replies
This commit is contained in:
Chris Sexton 2019-02-08 14:58:21 -05:00 committed by GitHub
commit 0bbfa88611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 15 deletions

View File

@ -5,8 +5,10 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"html" "html"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -46,9 +48,7 @@ func New(c *config.Config) *SlackApp {
log.Fatalf("No slack token found. Set SLACKTOKEN env.") log.Fatalf("No slack token found. Set SLACKTOKEN env.")
} }
dbg := slack.OptionDebug(true) api := slack.New(token, slack.OptionDebug(true))
api := slack.New(token)
dbg(api)
return &SlackApp{ return &SlackApp{
api: api, api: api,
@ -99,6 +99,8 @@ func (s *SlackApp) Serve() error {
case *slackevents.MessageEvent: case *slackevents.MessageEvent:
s.msgReceivd(ev) s.msgReceivd(ev)
} }
} else {
log.Printf("Event: (%v): %+v", eventsAPIEvent.Type, eventsAPIEvent)
} }
}) })
return nil return nil
@ -183,12 +185,48 @@ func (s *SlackApp) sendAction(channel, message string) (string, error) {
func (s *SlackApp) replyToMessageIdentifier(channel, message, identifier string) (string, error) { func (s *SlackApp) replyToMessageIdentifier(channel, message, identifier string) (string, error) {
nick := s.config.Get("Nick", "bot") nick := s.config.Get("Nick", "bot")
_, ts, err := s.api.PostMessage(channel, icon := s.config.Get("IconURL", "https://placekitten.com/128/128")
slack.MsgOptionUsername(nick),
slack.MsgOptionText(message, false), resp, err := http.PostForm("https://slack.com/api/chat.postMessage",
slack.MsgOptionMeMessage(), url.Values{"token": {s.botToken},
slack.MsgOptionTS(identifier)) "username": {nick},
return ts, err "icon_url": {icon},
"channel": {channel},
"text": {message},
"thread_ts": {identifier},
})
if err != nil {
err := fmt.Errorf("Error sending Slack reply: %s", err)
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
err := fmt.Errorf("Error reading Slack API body: %s", err)
return "", err
}
log.Println(string(body))
type MessageResponse struct {
OK bool `json:"ok"`
Timestamp string `json:"ts"`
}
var mr MessageResponse
err = json.Unmarshal(body, &mr)
if err != nil {
err := fmt.Errorf("Error parsing message response: %s", err)
return "", err
}
if !mr.OK {
return "", fmt.Errorf("Got !OK from slack message response")
}
return mr.Timestamp, err
} }
func (s *SlackApp) replyToMessage(channel, message string, replyTo msg.Message) (string, error) { func (s *SlackApp) replyToMessage(channel, message string, replyTo msg.Message) (string, error) {
@ -225,9 +263,7 @@ func (s *SlackApp) populateEmojiList() {
log.Println("Cannot get emoji list without slack.usertoken") log.Println("Cannot get emoji list without slack.usertoken")
return return
} }
dbg := slack.OptionDebug(true) api := slack.New(s.userToken, slack.OptionDebug(true))
api := slack.New(s.userToken)
dbg(api)
em, err := api.GetEmoji() em, err := api.GetEmoji()
if err != nil { if err != nil {
@ -303,9 +339,7 @@ func (s *SlackApp) Who(id string) []string {
log.Println("Cannot get emoji list without slack.usertoken") log.Println("Cannot get emoji list without slack.usertoken")
return []string{s.config.Get("nick", "bot")} return []string{s.config.Get("nick", "bot")}
} }
dbg := slack.OptionDebug(true) api := slack.New(s.userToken, slack.OptionDebug(true))
api := slack.New(s.userToken)
dbg(api)
log.Println("Who is queried for ", id) log.Println("Who is queried for ", id)
// Not super sure this is the correct call // Not super sure this is the correct call