~mendelmaleh/tg_nowpbot

58c56e53697bb3bf1f89b028f17624163e711500 — Mendel E 2 years ago master
Initial commit
2 files changed, 64 insertions(+), 0 deletions(-)

A .gitignore
A main.go
A  => .gitignore +1 -0
@@ 1,1 @@
config.toml

A  => main.go +63 -0
@@ 1,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,
			})
		}
	}
}