~mendelmaleh/tg_whoisbot

ref: 5581bdb057dcd23334a5c44211e35aa2ca9f58d9 tg_whoisbot/main.go -rw-r--r-- 2.6 KiB
5581bdb0Mendel E Formatting 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
}