From 53caadf9d9c1d805e16e40de097b2b27a00e2b21 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Sun, 27 Mar 2022 03:21:07 +0200 Subject: [PATCH] Add unsplash --- go.mod | 5 + go.sum | 20 +++ main.go | 19 ++- modules/module.go | 8 +- modules/unsplash/mod.go | 273 ++++++++++++++++++++++++++++++++++++++++ run.ps1 | 6 +- run.sh | 5 +- utils/text.go | 32 +++++ 8 files changed, 360 insertions(+), 8 deletions(-) create mode 100644 modules/unsplash/mod.go create mode 100644 utils/text.go diff --git a/go.mod b/go.mod index 6b48328..ec313e7 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,13 @@ module git.fromouter.space/crunchy-rocks/clessy-ng go 1.18 require ( + git.fromouter.space/crunchy-rocks/draw2d v0.0.0-20190208101535-675e82cb6412 + git.fromouter.space/crunchy-rocks/emoji v0.0.0-20181116142102-2188aadaf093 + git.fromouter.space/crunchy-rocks/freetype v0.0.0-20181116104610-3115318f2577 git.fromouter.space/hamcha/tg v0.1.0 git.sr.ht/~hamcha/containers v0.0.3 github.com/cockroachdb/pebble v0.0.0-20220323190648-7ad3551b2050 + github.com/disintegration/imaging v1.6.2 github.com/json-iterator/go v1.1.12 ) @@ -26,6 +30,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/stretchr/testify v1.7.0 // indirect golang.org/x/exp v0.0.0-20200513190911-00229845015e // indirect + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect diff --git a/go.sum b/go.sum index 89c9f46..59be4d5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,11 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +git.fromouter.space/crunchy-rocks/draw2d v0.0.0-20190208101535-675e82cb6412 h1:KvGMuKAnoIAPzx/wn0zi0bvOCuWlamw0MeBezlB3+VI= +git.fromouter.space/crunchy-rocks/draw2d v0.0.0-20190208101535-675e82cb6412/go.mod h1:aAujBHSXafZajIGoKnkq+Jzf11Io4yK+Wyh0dNAl4uY= +git.fromouter.space/crunchy-rocks/emoji v0.0.0-20181116142102-2188aadaf093 h1:paVIXZGSzj26O1EsVBRhDgE3hHX5nF9X5Wk1u3x9iDw= +git.fromouter.space/crunchy-rocks/emoji v0.0.0-20181116142102-2188aadaf093/go.mod h1:bmXZYbLNSGivLP7yQlcQ605b1H1L8kzxAk8A7sAEWx0= +git.fromouter.space/crunchy-rocks/freetype v0.0.0-20181116104610-3115318f2577 h1:rntW5kRu8s7dN8n7WWLHzAVZrnk7NS9YCdEQwA/O9pk= +git.fromouter.space/crunchy-rocks/freetype v0.0.0-20181116104610-3115318f2577/go.mod h1:MlK0wT7XDuAuw0l/Z+Es2OHozCbBb2r9VxOy8bU58ow= git.fromouter.space/hamcha/tg v0.1.0 h1:cJwL8pElkBtaDn7Bxa14zvlnBTTK8LdcCtcbBg7hEvk= git.fromouter.space/hamcha/tg v0.1.0/go.mod h1:aIFj7n5FP+Zr/Zv6I6Kq4ZqhRxC12gXFQcC3iOakv9M= git.sr.ht/~hamcha/containers v0.0.3 h1:obG9X8s5iOIahVe+EGpkBDYmUAO78oTi9Y9gRurt334= @@ -46,6 +52,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -64,6 +72,8 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/ github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98= github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= +github.com/go-gl/gl v0.0.0-20180407155706-68e253793080/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= +github.com/go-gl/glfw v0.0.0-20180426074136-46a8d530c326/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8= github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= @@ -74,6 +84,8 @@ github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/gogo/status v1.1.0/go.mod h1:BFv9nrluPLmrS0EmGVvLaPNmRosr9KapBYd5/hpY1WM= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -115,6 +127,7 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q= github.com/juju/loggo v0.0.0-20180524022052-584905176618/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U= github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA= +github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/kataras/golog v0.0.9/go.mod h1:12HJgwBIZFNGL0EJnMRhmvGA0PQGx8VFwrZtM4CqbAk= github.com/kataras/iris/v12 v12.0.1/go.mod h1:udK4vLQKkdDqMGJJVd/msuMtN6hpYJhg/lSzuxjhO+U= @@ -134,6 +147,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g= github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= +github.com/llgcode/draw2d v0.0.0-20180825133448-f52c8a71aff0 h1:2vp6ESimuT8pCuZHThVyV0hlfa9oPL06HnGCL9pbUgc= +github.com/llgcode/draw2d v0.0.0-20180825133448-f52c8a71aff0/go.mod h1:mVa0dA29Db2S4LVqDYLlsePDzRJLDfdhVZiI15uY0FA= +github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb h1:61ndUreYSlWFeCY44JxDDkngVoI7/1MVhEl98Nm0KOk= +github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb/go.mod h1:1l8ky+Ew27CMX29uG+a2hNOKpeNYEQjjtiALiBlFQbY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= @@ -213,8 +230,11 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20200513190911-00229845015e h1:rMqLP+9XLy+LdbCXHjJHAmTfXCr93W7oruWA6Hq1Alc= golang.org/x/exp v0.0.0-20200513190911-00229845015e/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw= +golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= diff --git a/main.go b/main.go index ab337cb..d5f19f9 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,10 @@ import ( "git.fromouter.space/crunchy-rocks/clessy-ng/modules/metafora" "git.fromouter.space/crunchy-rocks/clessy-ng/modules/proverbio" "git.fromouter.space/crunchy-rocks/clessy-ng/modules/remind" + "git.fromouter.space/crunchy-rocks/clessy-ng/modules/unsplash" "git.fromouter.space/crunchy-rocks/clessy-ng/utils" + "git.fromouter.space/crunchy-rocks/emoji" "git.fromouter.space/hamcha/tg" "github.com/cockroachdb/pebble" ) @@ -23,6 +25,7 @@ var mods = map[string]modules.Module{ "proverbio": &proverbio.Module{}, "macro": ¯o.Module{}, "remind": &remind.Module{}, + "unsplash": &unsplash.Module{}, } func checkErr(err error, message string, args ...interface{}) { @@ -44,6 +47,15 @@ func main() { name, err := api.GetMe() checkErr(err, "could not retrieve bot info") + // Load emojis + emojis, err := emoji.ScanEmojiDirectory(utils.RequireEnv("CLESSY_EMOJI_PATH")) + if err != nil { + log.Printf("[x-emoji] Error while loading emojis: %s\n", err.Error()) + log.Println("[x-emoji] Emoji support will be disabled") + } else { + log.Printf("[x-emoji] Loaded %d emojis\n", len(emojis)) + } + // Initialize your database db, err := pebble.Open(utils.RequireEnv("CLESSY_DB_DIR"), &pebble.Options{}) checkErr(err, "could not open database") @@ -85,9 +97,10 @@ func main() { for modname, mod := range toActivate { log.Printf("Initializing %s", modname) err := mod.Initialize(modules.ModuleOptions{ - API: api, - Name: name.Username, - KV: db, + API: api, + Name: name.Username, + KV: db, + Emojis: emojis, }) checkErr(err, "Starting module %s failed with error", modname) } diff --git a/modules/module.go b/modules/module.go index 268107a..0a45645 100644 --- a/modules/module.go +++ b/modules/module.go @@ -1,14 +1,16 @@ package modules import ( + "git.fromouter.space/crunchy-rocks/emoji" "git.fromouter.space/hamcha/tg" "github.com/cockroachdb/pebble" ) type ModuleOptions struct { - API *tg.Telegram - Name string - KV *pebble.DB + API *tg.Telegram + Name string + KV *pebble.DB + Emojis emoji.Table } type Module interface { diff --git a/modules/unsplash/mod.go b/modules/unsplash/mod.go new file mode 100644 index 0000000..4be278c --- /dev/null +++ b/modules/unsplash/mod.go @@ -0,0 +1,273 @@ +package unsplash + +import ( + "bytes" + "encoding/base64" + "image" + _ "image/gif" + "image/jpeg" + _ "image/png" + "io/ioutil" + "log" + "math/rand" + "os" + "path/filepath" + "strings" + + "git.fromouter.space/crunchy-rocks/clessy-ng/modules" + "git.fromouter.space/crunchy-rocks/clessy-ng/utils" + + "git.fromouter.space/crunchy-rocks/draw2d" + "git.fromouter.space/crunchy-rocks/draw2d/draw2dimg" + "git.fromouter.space/crunchy-rocks/emoji" + "git.fromouter.space/crunchy-rocks/freetype" + "git.fromouter.space/hamcha/tg" + "github.com/disintegration/imaging" +) + +var quoteFontData draw2d.FontData +var pics []string + +type Module struct { + client *tg.Telegram + name string + emojis emoji.Table +} + +func (m *Module) Initialize(options modules.ModuleOptions) error { + m.client = options.API + m.name = options.Name + m.emojis = options.Emojis + + fontfile := utils.RequireEnv("CLESSY_UNSPLASH_FONT") + bytes, err := os.ReadFile(fontfile) + if err != nil { + return err + } + + font, err := freetype.ParseFont(bytes) + if err != nil { + return err + } + + quoteFontData = draw2d.FontData{ + Name: "gillmt", + Family: draw2d.FontFamilySans, + Style: draw2d.FontStyleBold, + } + draw2d.RegisterFont(quoteFontData, font) + + // Read all the pictures inside a folder and save them for later + bgpath := utils.RequireEnv("CLESSY_UNSPLASH_BG_PATH") + files, err := ioutil.ReadDir(bgpath) + if err != nil { + return err + } + + for _, file := range files { + pics = append(pics, filepath.Join(bgpath, file.Name())) + } + + log.Printf("[unsplash] Loaded (%d available backgrounds)", len(pics)) + + return nil +} + +func (m *Module) OnUpdate(update tg.APIUpdate) { + // Not a message? Ignore + if update.Message == nil { + return + } + message := *update.Message + + if utils.IsCommand(message, m.name, "unsplash") { + text := "" + user := message.User + + if message.ReplyTo != nil { + switch { + case message.ReplyTo.Text != nil: + text = *(message.ReplyTo.Text) + case message.ReplyTo.Caption != nil: + text = *(message.ReplyTo.Caption) + default: + m.client.SendTextMessage(tg.ClientTextMessageData{ + ChatID: message.Chat.ChatID, + Text: "Non c'e' niente di 'ispiratore' in questo..", + ReplyID: &message.MessageID, + }) + return + } + + // For forwarded message take the original user + if message.FwdUser != nil { + user = *message.FwdUser + } else { + user = message.ReplyTo.User + } + } else { + if strings.Index(*(message.Text), " ") > 0 { + text = strings.TrimSpace(strings.SplitN(*(message.Text), " ", 2)[1]) + } + } + + // Cleanup chars + text = strings.Map(stripUnreadable, text) + + author := user.FirstName + if user.LastName != "" { + author = user.FirstName + " " + user.LastName + } + author += " (" + user.Username + ")" + + if strings.TrimSpace(text) == "" { + m.client.SendTextMessage(tg.ClientTextMessageData{ + ChatID: message.Chat.ChatID, + Text: "Non c'e' niente di 'ispiratore' in questo..", + ReplyID: &message.MessageID, + }) + return + } + + file, err := os.Open(pics[rand.Intn(len(pics))]) + if err != nil { + log.Printf("[unsplash] Could not open original image file: %s\n", err.Error()) + m.client.SendTextMessage(tg.ClientTextMessageData{ + ChatID: message.Chat.ChatID, + Text: "ERRORE! @hamcha controlla la console!", + ReplyID: &message.MessageID, + }) + return + } + defer file.Close() + + img, _, err := image.Decode(file) + if err != nil { + log.Printf("[unsplash] Image decode error: %s\n", err.Error()) + m.client.SendTextMessage(tg.ClientTextMessageData{ + ChatID: message.Chat.ChatID, + Text: "ERRORE! @hamcha controlla la console!", + ReplyID: &message.MessageID, + }) + return + } + + m.client.SendChatAction(tg.ClientChatActionData{ + ChatID: message.Chat.ChatID, + Action: tg.ActionUploadingPhoto, + }) + + // Darken image + img = imaging.AdjustBrightness(imaging.AdjustGamma(imaging.AdjustSigmoid(img, 0.5, -6.0), 0.8), -20) + + // Create target image + bounds := img.Bounds() + iwidth := float64(bounds.Size().X) + iheight := float64(bounds.Size().Y) + + timg := image.NewRGBA(bounds) + gc := draw2dimg.NewGraphicContext(timg) + gc.Emojis = m.emojis + gc.SetFontData(quoteFontData) + gc.DrawImage(img) + gc.SetStrokeColor(image.Black) + gc.SetFillColor(image.White) + + text = strings.ToUpper(strings.TrimSpace(text)) + gc.Restore() + gc.Save() + + // Detect appropriate font size + scale := iheight / iwidth * (iwidth / 10) * 0.8 + gc.SetFontSize(scale) + gc.SetLineWidth(scale / 15) + + // Get NEW bounds + left, top, right, bottom := gc.GetStringBounds(text) + + width := right - left + texts := []string{text} + if width*1.2 > iwidth { + // Split text + texts = utils.SplitCenter(text) + + // Get longest line + longer := float64(0) + longid := 0 + widths := make([]float64, len(texts)) + for id := range texts { + tleft, _, tright, _ := gc.GetStringBounds(texts[id]) + widths[id] = tright - tleft + if width > longer { + longer = widths[id] + longid = id + } + } + + // Still too big? Decrease font size again + iter := 0 + for width*1.2 > iwidth && iter < 10 { + scale *= (0.9 - 0.05*float64(iter)) + gc.SetFontSize(scale) + left, top, right, bottom = gc.GetStringBounds(texts[longid]) + width = right - left + iter++ + } + } + + texts = append(texts, author) + height := bottom - top + 20 + margin := float64(height / 50) + txtheight := (height + margin) * float64(len(texts)) + + gc.Save() + for id, txt := range texts { + gc.Save() + left, _, right, _ = gc.GetStringBounds(txt) + width = right - left + + x := (iwidth - width) / 2 + y := (iheight-txtheight)/2 + (height+margin*2)*float64(id+1) + if id == len(texts)-1 { + gc.SetFontSize(scale * 0.7) + left, _, right, _ = gc.GetStringBounds(txt) + width = right - left + x = (iwidth - width) / 1.5 + y = (iheight-txtheight)/2 + (height+margin)*float64(id+1) + margin*6 + } + + gc.Translate(x, y) + gc.StrokeString(txt) + gc.FillString(txt) + gc.Restore() + } + + buf := new(bytes.Buffer) + err = jpeg.Encode(buf, timg, &(jpeg.Options{Quality: 80})) + if err != nil { + log.Printf("[unsplash] Image encode error: %s\n", err.Error()) + m.client.SendTextMessage(tg.ClientTextMessageData{ + ChatID: message.Chat.ChatID, + Text: "ERRORE! @hamcha controlla la console!", + ReplyID: &message.MessageID, + }) + return + } + m.client.SendPhoto(tg.ClientPhotoData{ + ChatID: message.Chat.ChatID, + Bytes: base64.StdEncoding.EncodeToString(buf.Bytes()), + Filename: "quote.jpg", + ReplyID: &message.MessageID, + }) + } +} + +func stripUnreadable(r rune) rune { + if r == '\n' || r == '\t' { + return ' ' + } + if r < 32 { + return -1 + } + return r +} diff --git a/run.ps1 b/run.ps1 index 1c0d32b..40dc47b 100644 --- a/run.ps1 +++ b/run.ps1 @@ -2,5 +2,9 @@ $env:CLESSY_TOKEN = $args[0] $env:CLESSY_WEBHOOK = $args[1] + "/test" $env:CLESSY_DB_DIR = "_data/db" -mkdir -force _data +$env:CLESSY_EMOJI_PATH = "_data" +$env:CLESSY_UNSPLASH_FONT = "_data/gill.ttf" +$env:CLESSY_UNSPLASH_BG_PATH = "_data/pics" +$env:CLESSY_MEME_FONT = "_data/impact.ttf" +mkdir -force _data/pics go run . \ No newline at end of file diff --git a/run.sh b/run.sh index 131ad2a..f493b65 100644 --- a/run.sh +++ b/run.sh @@ -2,5 +2,8 @@ export CLESSY_TOKEN=$1 export CLESSY_WEBHOOK=$2/test export CLESSY_DB_DIR=_data/db -mkdir -p _data +export CLESSY_EMOJI_PATH=_data +export CLESSY_UNSPLASH_FONT=_data/gill.ttf +export CLESSY_MEME_FONT=_data/impact.ttf +mkdir -p _data/pics go run . \ No newline at end of file diff --git a/utils/text.go b/utils/text.go new file mode 100644 index 0000000..b3f0af1 --- /dev/null +++ b/utils/text.go @@ -0,0 +1,32 @@ +package utils + +import "strings" + +func abs(i int) int { + if i < 0 { + return -i + } + return i +} + +func SplitCenter(text string) []string { + centerIndex := int(len(text) / 2) + whitespaceFrontIndex := strings.IndexRune(text[centerIndex:], ' ') + whitespaceBackIndex := strings.LastIndex(text[:centerIndex], " ") + frontIndex := centerIndex + whitespaceFrontIndex + frontDiff := 9999 + backDiff := 9999 + if whitespaceFrontIndex < 1 && whitespaceBackIndex < 1 { + return []string{text} + } + if whitespaceFrontIndex >= 0 { + frontDiff = len(text[:frontIndex]) - len(text[frontIndex:]) + } + if whitespaceBackIndex >= 0 { + backDiff = len(text[:whitespaceBackIndex]) - len(text[whitespaceBackIndex:]) + } + if abs(frontDiff) < abs(backDiff) { + return []string{strings.TrimSpace(text[:frontIndex]), strings.TrimSpace(text[frontIndex:])} + } + return []string{strings.TrimSpace(text[:whitespaceBackIndex]), strings.TrimSpace(text[whitespaceBackIndex:])} +}