Skip to content

Commit

Permalink
Telereto accepts links to photos now.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhboner committed Apr 26, 2022
1 parent ab8b570 commit 663e6e2
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion telereto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"golang.org/x/exp/slices"
tele "gopkg.in/telebot.v3"
"log"
"net/http"
"net/url"
)

Expand Down Expand Up @@ -49,8 +50,21 @@ func main() {
}

bot.Handle(tele.OnText, func(context tele.Context) error {
// TODO: Text to image
// If a text is an url, try to fetch and upload that image.
text := context.Text()
parsed_url, err := url.ParseRequestURI(text)
if err != nil {
return context.Send("请上传一张图片")
}

url_string := parsed_url.String()
resp, err := http.Get(url_string)
if err != nil {
return context.Send("请上传一张图片")
}
if slices.Contains(ACCEPTABLE_TYPES, resp.Header.Get("Content-Type")) {
return context.Send(upload_photo(url_string))
}
return context.Send("请上传一张图片")
})

Expand Down

0 comments on commit 663e6e2

Please sign in to comment.