~mendelmaleh/tg_nowpbot

ref: 58c56e53697bb3bf1f89b028f17624163e711500 tg_nowpbot/main.go -rw-r--r-- 1.2 KiB
58c56e53Mendel E Initial commit 2 years 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
package main

import (
	"io/ioutil"
	"log"

	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
	"github.com/pelletier/go-toml"
)

type Config struct {
	Bot struct {
		Token string
	}
}

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

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

	bot, err := tgbotapi.NewBotAPI(config.Bot.Token)
	if err != nil {
		log.Panic(err)
	}

	log.Printf("Authorized on account %s", bot.Self.UserName)
	updates, err := bot.GetUpdatesChan(tgbotapi.UpdateConfig{Timeout: 60})
	if err != nil {
		log.Panic(err)
	}

	for u := range updates {
		if u.InlineQuery != nil {
			q := u.InlineQuery
			r := tgbotapi.InlineQueryResultArticle{
				Type:        "article", // must be
				ID:          "0",
				Title:       "No Web Preview",
				Description: q.Query,
				InputMessageContent: tgbotapi.InputTextMessageContent{
					Text:                  q.Query,
					DisableWebPagePreview: true,
				},
			}

			results := make([]interface{}, 1)
			results[0] = r

			bot.AnswerInlineQuery(tgbotapi.InlineConfig{
				InlineQueryID: q.ID,
				Results:       results,
			})
		}
	}
}