~mendelmaleh/tg_whoisbot

ad45a56e2cee2463e2980e96e9effc47a55b4aba — Mendel E 2 years ago
Initial commit
3 files changed, 160 insertions(+), 0 deletions(-)

A go.mod
A go.sum
A main.go
A  => go.mod +17 -0
@@ 1,17 @@
module git.sr.ht/~mendelmaleh/tg_whoisbot

go 1.17

require (
	github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.6
	github.com/bobesa/go-domain-util v0.0.0-20190911083921-4033b5f7dd89
	github.com/likexian/whois v1.12.4
	github.com/likexian/whois-parser v1.22.0
	github.com/pelletier/go-toml v1.9.4
)

require (
	github.com/likexian/gokit v0.25.6 // indirect
	golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
	golang.org/x/text v0.3.7 // indirect
)

A  => go.sum +28 -0
@@ 1,28 @@
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.6 h1:MoFt/Ff7+IX9Qq/w67xHV3UFSyhbctjHhd/pq+ujQr0=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.6/go.mod h1:r815fYWTudnU9JhtsJAxUtuV7QrSgKpChJkfTSMFpfg=
github.com/bobesa/go-domain-util v0.0.0-20190911083921-4033b5f7dd89 h1:2pkAuIM8OF1fy4ToFpMnI4oE+VeUNRbGrpSLKshK0oQ=
github.com/bobesa/go-domain-util v0.0.0-20190911083921-4033b5f7dd89/go.mod h1:/09nEjna1UMoasyyQDhOrIn8hi2v2kiJglPWed1idck=
github.com/likexian/gokit v0.25.6 h1:DZuMrmfgXErhdfI9SIS6tVMZ5QbRMP3aruHNq5lGcMI=
github.com/likexian/gokit v0.25.6/go.mod h1:q1LC+z3cBymJuE4oeiWiIPhJceUa0nptg4Id8tSzjZI=
github.com/likexian/whois v1.12.4 h1:NqUNc9LC4G5Fq62o6a3nw/HO8haJDULEudcJPyPMwXg=
github.com/likexian/whois v1.12.4/go.mod h1:SfdfmB72mSdrC/8eLjYkeaEJp9t1MPAgp0ebCzZfYXw=
github.com/likexian/whois-parser v1.22.0 h1:YXSNvDNBy+cZG+2JZWJvWbMHrDY35r6Etpu1TNDPYW0=
github.com/likexian/whois-parser v1.22.0/go.mod h1:2bJqtH4tNPanBvOp/3Kj3Sd12S9vxTbsJ0+0zjRc3ow=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

A  => main.go +115 -0
@@ 1,115 @@
package main

import (
	"fmt"
	"html"
	"log"
	"os"
	"strings"

	"github.com/PaulSonOfLars/gotgbot/v2"
	"github.com/PaulSonOfLars/gotgbot/v2/ext"
	"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
	"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters/inlinequery"
	"github.com/bobesa/go-domain-util/domainutil"
	"github.com/likexian/whois"
	whoisparser "github.com/likexian/whois-parser"
	"github.com/pelletier/go-toml"
)

type Config struct {
	Token string
}

func main() {
	doc, err := os.ReadFile("config.toml")
	if err != nil {
		log.Fatal(err)
	}

	var config Config
	if err := toml.Unmarshal(doc, &config); err != nil {
		log.Fatal(err)
	}

	bot, err := gotgbot.NewBot(config.Token, nil)
	if err != nil {
		log.Fatal(err)
	}

	user, err := bot.GetMe()
	if err != nil {
		log.Fatal(err)
	}

	updater := ext.NewUpdater(&ext.UpdaterOpts{
		DispatcherOpts: ext.DispatcherOpts{
			Error: func(bot *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
				log.Println("error handling update:", err)
				return ext.DispatcherActionNoop
			},
		},
	})

	dispatcher := updater.Dispatcher
	dispatcher.AddHandler(handlers.NewInlineQuery(inlinequery.All, handler))

	if err := updater.StartPolling(bot, &ext.PollingOpts{
		GetUpdatesOpts: gotgbot.GetUpdatesOpts{
			AllowedUpdates: []string{},
		},
	}); err != nil {
		log.Fatal("failed to start polling:", err)
	}

	log.Printf("started @%s", user.Username)
	updater.Idle()
}

func handler(bot *gotgbot.Bot, ctx *ext.Context) error {
	q := ctx.Update.InlineQuery

	domain := domainutil.Domain(q.Query)
	if domain == "" {
		log.Printf("query from [%d]: %q, no domain", q.From.Id, q.Query)
		return nil
	}

	raw, err := whois.Whois(domain)
	if err != nil {
		log.Printf("query from [%d]: %q, domain: %q, whois err", q.From.Id, q.Query, domain)
		return err
	}

	w, err := whoisparser.Parse(raw)
	if err != nil {
		log.Printf("query from [%d]: %q, domain: %q, whois parsing err", q.From.Id, q.Query, domain)
		return err
	}

	_, err = q.Answer(bot, []gotgbot.InlineQueryResult{gotgbot.InlineQueryResultArticle{
		Id:    q.Query,
		Title: domain,
		InputMessageContent: gotgbot.InputTextMessageContent{
			ParseMode:             "HTML",
			DisableWebPagePreview: true,
			MessageText: fmt.Sprintf(
				strings.Join([]string{
					"<b>Domain:</b> %s",
					"<b>Registrar:</b> <a href=\"%s\">%s</a>",
					"<b>Name servers:</b> %s",
					"<b>Full WHOIS:</b> https://1api.net/whois?query=%s",
				}, "\n"),
				domain,
				w.Registrar.ReferralURL,
				html.EscapeString(w.Registrar.Name),
				strings.Join(w.Domain.NameServers, ", "),
				domain,
			),
		},
	}}, nil)

	log.Printf("query from [%d]: %q, domain: %q, whois success", q.From.Id, q.Query, domain)

	return err
}