add files

main
astravexton 2022-10-07 10:43:14 +00:00
commit de8a92c967
4 changed files with 209 additions and 0 deletions

18
Dockerfile 100644
View File

@ -0,0 +1,18 @@
FROM golang:alpine AS builder
WORKDIR /go/src/git.zio.sh/astra/ddgimagebot
COPY . .
RUN apk update && \
apk add --no-cache git bash && \
go get -d -v ./... && \
go install
FROM alpine:latest
COPY --from=builder /go/bin/ddgimagebot /usr/local/bin/ddgimagebot
RUN apk add --no-cache bash
ENV BOT_TOKEN ""
CMD ["ddgimagebot"]

15
go.mod 100644
View File

@ -0,0 +1,15 @@
module git.zio.sh/astra/ddgimagebot
go 1.19
require (
git.zio.sh/astra/telegram-bot-api v1.0.1-0.20220824135549-c84603be8b0f
github.com/astravexton/logger v0.2.2-0.20211216142523-c1e08a465709
github.com/dghubble/sling v1.4.0
)
require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
)

24
go.sum 100644
View File

@ -0,0 +1,24 @@
git.zio.sh/astra/telegram-bot-api v1.0.1-0.20220824135549-c84603be8b0f h1:HD6LcxkXXFfzWUeYm5YsVNsoUZFVrd2/jgMGgiYWRMI=
git.zio.sh/astra/telegram-bot-api v1.0.1-0.20220824135549-c84603be8b0f/go.mod h1:gEnVclR6CKL/hj3SZV2lmQzt4Ul0Chq8xkjBBKzdxOE=
git.zio.sh/astra/telegram-bot-api v3.0.0+incompatible h1:OirhOPiMKlH9M8WCILFX69S43pAiAhr9rIxkCY7TVFs=
git.zio.sh/astra/telegram-bot-api v3.0.0+incompatible/go.mod h1:kTm5G2y4tw4tlnUps6VTc5QwNEhdgTD+qErZhd+2W0o=
github.com/astravexton/logger v0.2.1 h1:nYxhV6oCHIJzUzaPhawbAc51EEvJowIViVsB5EbdeJQ=
github.com/astravexton/logger v0.2.1/go.mod h1:6uxVhLlWlvZn48W8F52dsMXt4yztlrInpRhdWiJ9RXc=
github.com/astravexton/logger v0.2.2-0.20211216142523-c1e08a465709 h1:S+dBCax7wdg3ikWx5t6cClPKQIerrmD404qAzWhO2V4=
github.com/astravexton/logger v0.2.2-0.20211216142523-c1e08a465709/go.mod h1:6BlG5E0U3CImXLmDtGpEwdoc8PHiiDv0kbvJBCbfiMY=
github.com/dghubble/sling v1.4.0 h1:/n8MRosVTthvMbwlNZgLx579OGVjUOy3GNEv5BIqAWY=
github.com/dghubble/sling v1.4.0/go.mod h1:0r40aNsU9EdDUVBNhfCstAtFgutjgJGYbO1oNzkMoM8=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kris-nova/lolgopher v0.0.0-20210112022122-73f0047e8b65/go.mod h1:V0HF/ZBlN86HqewcDC/cVxMmYDiRukWjSrgKLUAn9Js=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

152
main.go 100644
View File

@ -0,0 +1,152 @@
package main
import (
"io"
"net/http"
"os"
"regexp"
"strings"
"time"
tgbotapi "git.zio.sh/astra/telegram-bot-api"
"github.com/astravexton/logger"
"github.com/dghubble/sling"
"github.com/google/uuid"
)
const ddgURL = "https://duckduckgo.com"
// ImgResult ...
type ImgResult struct {
Ads interface{} `json:"ads"`
Next string `json:"next"`
Query string `json:"query"`
QueryEncoded string `json:"queryEncoded"`
ResponseType string `json:"response_type"`
Results []Results `json:"results"`
Vqd struct {
Testing string `json:"testing"`
} `json:"vqd"`
}
type Results struct {
Height int `json:"height"`
Image string `json:"image"`
Source string `json:"source"`
Thumbnail string `json:"thumbnail"`
Title string `json:"title"`
URL string `json:"url"`
Width int `json:"width"`
}
type Params struct {
Query string `url:"q,omitempty"`
L string `url:"l,omitempty"`
O string `url:"o,omitempty"`
Q string `url:"q,omitempty"`
VQD string `url:"vqd,omitempty"`
F string `url:"f,omitempty"`
P string `url:"p,omitempty"`
S string `url:"s,omitempty"`
U string `url:"u,omitempty"`
}
func main() {
logger.Layout = time.Stamp
logger.BitwiseLevel = logger.LogEverything
bot, err := tgbotapi.NewBotAPI(os.Getenv("BOT_TOKEN"))
if err != nil {
logger.Critical(err.Error())
}
me, err := bot.GetMe()
if err != nil {
logger.Critical(err.Error())
}
logger.Info("Authorized as %s", me.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.InlineQuery != nil {
if update.InlineQuery.Query != "" {
logger.Info("Inline query %q from %s", update.InlineQuery.Query, update.InlineQuery.From)
var results []interface{}
images := getImageList(update.InlineQuery.Query)
for c, image := range images {
if c == 50 {
break
}
result := &tgbotapi.InlineQueryResultPhoto{
Type: "photo",
ID: uuid.New().String(),
URL: image.Image,
ThumbURL: image.Thumbnail,
}
results = append(results, result)
}
inlineConf := tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: false,
CacheTime: 1,
Results: results,
}
bot.Request(inlineConf)
}
}
}
}
func getImageList(query string) []Results {
params := &Params{Query: query}
s := sling.New().Get(ddgURL)
s.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36")
s.QueryStruct(params)
req, err := s.Request()
if err != nil {
logger.Critical(err.Error())
}
client := &http.Client{}
client.Timeout = time.Second * 3
res, err := client.Do(req)
if err != nil {
logger.Critical(err.Error())
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
bodyContents := string(body)
r := regexp.MustCompile(`vqd=([\d-]+)`)
token := strings.ReplaceAll(r.FindString(bodyContents), "vqd=", "")
params = &Params{
L: "us-en",
O: "json",
Q: query,
VQD: token,
F: ",,,,,",
P: "-1",
S: "0",
U: "bing",
}
s = s.Get(ddgURL).Path("/i.js").QueryStruct(params)
s.Set("Authority", "duckduckgo.com")
s.Set("Accept", "application/json, text/javascript, */*; q=0.01")
s.Set("Cookie", "bc=1; at=-1; au=-1; av=1; ax=-1; ak=-1; ao=-1; ap=-1; 1=-1; 5=1; aq=-1; ay=b; ah=uk-en; l=wt-wt; p=-2")
s.Set("Referer", "https://duckduckgo.com/")
imageList := &ImgResult{}
_, err = s.ReceiveSuccess(imageList)
if err != nil {
logger.Critical(err.Error())
}
return imageList.Results
}