Better title and notes formatting
This commit is contained in:
5
asana.go
5
asana.go
@@ -3,8 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@@ -69,7 +69,8 @@ func (ac *AsanaClient) CreateTask(name string, notes string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 201 {
|
if resp.StatusCode != 201 {
|
||||||
return errors.New(resp.Status)
|
msg, _ := io.ReadAll(resp.Body)
|
||||||
|
return fmt.Errorf("%s: %s", resp.Status, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
50
slack.go
50
slack.go
@@ -229,24 +229,56 @@ func (sc *SlackClient) RemoveStar(item *Item) error {
|
|||||||
func (sc *SlackClient) GetTitle(item *Item, user *User, channel *Channel) (string, error) {
|
func (sc *SlackClient) GetTitle(item *Item, user *User, channel *Channel) (string, error) {
|
||||||
switch {
|
switch {
|
||||||
case channel.IsIm:
|
case channel.IsIm:
|
||||||
return fmt.Sprintf("[%s] %s", user.Name, item.Message.Text), nil
|
return fmt.Sprintf("<%s> %s", user.Name, item.Message.Text), nil
|
||||||
case channel.IsMpIm:
|
case channel.IsMpIm:
|
||||||
return fmt.Sprintf("[%s] %s", user.Name, item.Message.Text), nil
|
return fmt.Sprintf("<%s> %s", user.Name, item.Message.Text), nil
|
||||||
case channel.IsChannel:
|
case channel.IsChannel:
|
||||||
return fmt.Sprintf("[%s] %s", user.Name, item.Message.Text), nil
|
return fmt.Sprintf("<%s> %s", user.Name, item.Message.Text), nil
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("unknown channel type: %#v", channel)
|
return "", fmt.Errorf("unknown channel type: %#v", channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sc *SlackClient) GetTrimmedTitle(item *Item, user *User, channel *Channel) (string, error) {
|
||||||
|
title, err := sc.GetTitle(item, user, channel)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.SplitN(title, "\n", 2)
|
||||||
|
title = parts[0]
|
||||||
|
|
||||||
|
if len(title) < 80 {
|
||||||
|
return title, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s...", title[:77]), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (sc *SlackClient) GetNotes(item *Item, user *User, channel *Channel) (string, error) {
|
func (sc *SlackClient) GetNotes(item *Item, user *User, channel *Channel) (string, error) {
|
||||||
|
title, err := sc.GetTitle(item, user, channel)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case channel.IsIm:
|
case channel.IsIm:
|
||||||
return "", nil
|
return fmt.Sprintf(
|
||||||
|
"<body>%s</body>",
|
||||||
|
sc.escape(title),
|
||||||
|
), nil
|
||||||
case channel.IsMpIm:
|
case channel.IsMpIm:
|
||||||
return fmt.Sprintf("<body>In %s</body>", sc.getTaggedNamesString(channel.Purpose.Value)), nil
|
return fmt.Sprintf(
|
||||||
|
"<body>%s\n\nIn %s</body>",
|
||||||
|
sc.escape(title),
|
||||||
|
sc.escape(sc.getTaggedNamesString(channel.Purpose.Value)),
|
||||||
|
), nil
|
||||||
case channel.IsChannel:
|
case channel.IsChannel:
|
||||||
return fmt.Sprintf("<body>In #%s</body>", channel.Name), nil
|
return fmt.Sprintf(
|
||||||
|
"<body>%s\n\nIn #%s</body>",
|
||||||
|
sc.escape(title),
|
||||||
|
sc.escape(channel.Name),
|
||||||
|
), nil
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("unknown channel type: %#v", channel)
|
return "", fmt.Errorf("unknown channel type: %#v", channel)
|
||||||
}
|
}
|
||||||
@@ -262,6 +294,12 @@ func (sc *SlackClient) getTaggedNamesString(in string) string {
|
|||||||
return fmt.Sprintf("{%s}", strings.Join(sc.getTaggedNames(in), ","))
|
return fmt.Sprintf("{%s}", strings.Join(sc.getTaggedNames(in), ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sc *SlackClient) escape(in string) string {
|
||||||
|
in = strings.ReplaceAll(in, "<", "<")
|
||||||
|
in = strings.ReplaceAll(in, ">", ">")
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
|
||||||
func (sc *SlackClient) addAuth(req *http.Request) {
|
func (sc *SlackClient) addAuth(req *http.Request) {
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", sc.token))
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", sc.token))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ func Poll(ac *AsanaClient, sc *SlackClient) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
title, err := sc.GetTitle(item, user, channel)
|
title, err := sc.GetTrimmedTitle(item, user, channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user