Option to include message from Twitter.
This commit is contained in:
parent
1df6a63ad4
commit
efa14e9695
|
@ -29,7 +29,7 @@ func (i inline) Execute(f *finch.Finch, query tgbotapi.InlineQuery) error {
|
|||
err = e
|
||||
if posts != nil && len(posts) != 0 {
|
||||
for _, post := range posts {
|
||||
results = append(results, post.BuildInlineResponse())
|
||||
results = append(results, post.BuildInlineResponse()...)
|
||||
}
|
||||
}
|
||||
break
|
||||
|
|
|
@ -21,10 +21,7 @@ var dbFolder = os.Getenv("DB_DIR")
|
|||
func ensureFolderExists() {
|
||||
_, err := os.Stat(dbFolder)
|
||||
if os.IsNotExist(err) {
|
||||
err = os.Mkdir(dbFolder, 0600)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.Mkdir(dbFolder, 0600)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
1
go.mod
1
go.mod
|
@ -12,6 +12,7 @@ require (
|
|||
github.com/dghubble/sling v1.1.0 // indirect
|
||||
github.com/getsentry/raven-go v0.0.0-20180903072508-084a9de9eb03 // indirect
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.3-0.20180922012028-898e79fe47da+incompatible
|
||||
github.com/gofrs/uuid v3.1.0+incompatible
|
||||
github.com/google/go-querystring v1.0.0 // indirect
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
|
||||
github.com/pkg/errors v0.8.0 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -26,6 +26,8 @@ github.com/go-telegram-bot-api/telegram-bot-api v4.6.2+incompatible h1:tI1+S63ai
|
|||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.2+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.3-0.20180922012028-898e79fe47da+incompatible h1:i3CbDUEGfMiKkGw7bcsXmuIFYQmHE0XoZw9xDV/OKbE=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api v4.6.3-0.20180922012028-898e79fe47da+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
|
||||
github.com/gofrs/uuid v3.1.0+incompatible h1:q2rtkjaKT4YEr6E1kamy0Ha4RtepWlQBedyHx0uzKwA=
|
||||
github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe h1:CHRGQ8V7OlCYtwaKPJi3iA7J+YdNKdo8j7nG5IgDhjs=
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package sites
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
|
||||
"github.com/Syfaro/finch"
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
"github.com/gofrs/uuid"
|
||||
"huefox.com/syfaro/telegram-furryimgbot/logger"
|
||||
)
|
||||
|
||||
// Site is a supported site to load an image from.
|
||||
|
@ -23,6 +23,7 @@ type PostInfo struct {
|
|||
FullURL string
|
||||
Thumb string
|
||||
Caption string
|
||||
Message string
|
||||
}
|
||||
|
||||
func getKeyboard(sourceURL, imageURL string) *tgbotapi.InlineKeyboardMarkup {
|
||||
|
@ -48,34 +49,56 @@ func getFileExt(name string) string {
|
|||
}
|
||||
|
||||
// BuildInlineResponse creates an inline response from a PostInfo struct.
|
||||
func (post PostInfo) BuildInlineResponse() interface{} {
|
||||
postID := hashURL(post.URL)
|
||||
func (post PostInfo) BuildInlineResponse() []interface{} {
|
||||
postID := genID()
|
||||
|
||||
fullURL := post.FullURL
|
||||
if fullURL == "" {
|
||||
fullURL = post.URL
|
||||
}
|
||||
|
||||
var results []interface{}
|
||||
|
||||
switch post.FileType {
|
||||
case "gif":
|
||||
return tgbotapi.InlineQueryResultGIF{
|
||||
results = append(results, tgbotapi.InlineQueryResultGIF{
|
||||
Type: "gif",
|
||||
ID: postID,
|
||||
URL: post.URL,
|
||||
ThumbURL: post.Thumb,
|
||||
ReplyMarkup: getKeyboard(post.Caption, fullURL),
|
||||
}
|
||||
})
|
||||
case "webm", "swf":
|
||||
return tgbotapi.NewInlineQueryResultArticle("unknown", "Unsupported Type", "unsupported")
|
||||
results = append(results, tgbotapi.NewInlineQueryResultArticle("unknown", "Unsupported Type", "unsupported"))
|
||||
default:
|
||||
return tgbotapi.InlineQueryResultPhoto{
|
||||
results = append(results, tgbotapi.InlineQueryResultPhoto{
|
||||
Type: "photo",
|
||||
ID: postID,
|
||||
URL: post.URL,
|
||||
ThumbURL: post.Thumb,
|
||||
ReplyMarkup: getKeyboard(post.Caption, fullURL),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if _, ok := results[0].(tgbotapi.InlineQueryResultArticle); ok {
|
||||
logger.Log.Debug("Result was article, skipping message check")
|
||||
return results
|
||||
}
|
||||
|
||||
if post.Message == "" {
|
||||
logger.Log.Debug("Photo did not have extra message")
|
||||
return results
|
||||
}
|
||||
|
||||
logger.Log.Debug("Adding message to photo")
|
||||
|
||||
messageItem := results[0].(tgbotapi.InlineQueryResultPhoto)
|
||||
messageItem.ID = genID()
|
||||
messageItem.Caption = post.Message
|
||||
|
||||
results = append(results, messageItem)
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// KnownSites are the sites we should operate on.
|
||||
|
@ -87,13 +110,9 @@ var KnownSites = []Site{
|
|||
&direct{},
|
||||
}
|
||||
|
||||
func hashURL(url string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(url))
|
||||
hs := h.Sum(nil)
|
||||
sh := hex.EncodeToString(hs)
|
||||
|
||||
return string(sh)[:32]
|
||||
func genID() string {
|
||||
u, _ := uuid.NewV4()
|
||||
return u.String()
|
||||
}
|
||||
|
||||
// ConfigureSites initializes the configuration for each site.
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Syfaro/finch"
|
||||
"github.com/boltdb/bolt"
|
||||
|
@ -44,6 +45,36 @@ func (twit) IsSupportedURL(url string) bool {
|
|||
return twitMatch.MatchString(url)
|
||||
}
|
||||
|
||||
func toChar(i int) rune {
|
||||
return rune('A' + i)
|
||||
}
|
||||
|
||||
// humanifyURLs is a horribly hacky way to replace all the Twitter URLs with
|
||||
// the non-shortened version.
|
||||
func humanifyURLs(text string, urls []twitter.URLEntity) string {
|
||||
newStr := text
|
||||
|
||||
replacements := make(map[string]string)
|
||||
|
||||
for idx, url := range urls {
|
||||
indices := url.Indices
|
||||
length := url.Indices[1] - url.Indices[0]
|
||||
|
||||
letter := toChar(idx)
|
||||
replacement := strings.Repeat(string(letter), length)
|
||||
|
||||
newStr = newStr[:url.Indices[0]] + replacement + newStr[indices[1]:]
|
||||
|
||||
replacements[replacement] = url.ExpandedURL
|
||||
}
|
||||
|
||||
for str, val := range replacements {
|
||||
newStr = strings.Replace(newStr, str, val, 1)
|
||||
}
|
||||
|
||||
return newStr
|
||||
}
|
||||
|
||||
func (t twit) GetImageURLs(postURL string, user tgbotapi.User) ([]PostInfo, error) {
|
||||
match := twitMatch.FindStringSubmatch(postURL)
|
||||
if match == nil {
|
||||
|
@ -121,6 +152,7 @@ func (t twit) GetImageURLs(postURL string, user tgbotapi.User) ([]PostInfo, erro
|
|||
URL: entity.MediaURLHttps,
|
||||
Thumb: entity.MediaURLHttps,
|
||||
Caption: entity.ExpandedURL,
|
||||
Message: humanifyURLs(status.FullText, status.Entities.Urls),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package sites
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/dghubble/go-twitter/twitter"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var testStr = "testing something \n\nhttps://t.co/7Prvj7hYPd\n\npls ignore \n\nhttps://t.co/4myFJksuKR\n\n:3 https://t.co/YERKTUlNOZ"
|
||||
|
||||
var newStr = "testing something \n\nhttps://syfaro.net/\n\npls ignore \n\nhttps://www.furaffinity.net/user/syfaro\n\n:3 https://t.co/YERKTUlNOZ"
|
||||
|
||||
func TestHumanifyURLs(t *testing.T) {
|
||||
str := humanifyURLs(testStr, []twitter.URLEntity{
|
||||
twitter.URLEntity{
|
||||
Indices: twitter.Indices{20, 43},
|
||||
DisplayURL: "syfaro.net",
|
||||
ExpandedURL: "https://syfaro.net/",
|
||||
URL: "https://t.co/7Prvj7hYPd",
|
||||
},
|
||||
twitter.URLEntity{
|
||||
Indices: twitter.Indices{58, 81},
|
||||
DisplayURL: "furaffinity.net/user/syfaro",
|
||||
ExpandedURL: "https://www.furaffinity.net/user/syfaro",
|
||||
URL: "https://t.co/4myFJksuKR",
|
||||
},
|
||||
})
|
||||
|
||||
assert.Equal(t, newStr, str)
|
||||
}
|
Loading…
Reference in New Issue