Skip to content

Instantly share code, notes, and snippets.

@zaz600
Last active November 10, 2022 12:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zaz600/b377834ef863de563bb1 to your computer and use it in GitHub Desktop.
Save zaz600/b377834ef863de563bb1 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/Syfaro/telegram-bot-api"
"log"
)
func main() {
// подключаемся к боту с помощью токена
bot, err := tgbotapi.NewBotAPI("ТОКЕН")
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
// инициализируем канал, куда будут прилетать обновления от API
var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
ucfg.Timeout = 60
err = bot.UpdatesChan(ucfg)
// читаем обновления из канала
for {
select {
case update := <-bot.Updates:
// Пользователь, который написал боту
UserName := update.Message.From.UserName
// ID чата/диалога.
// Может быть идентификатором как чата с пользователем
// (тогда он равен UserID) так и публичного чата/канала
ChatID := update.Message.Chat.ID
// Текст сообщения
Text := update.Message.Text
log.Printf("[%s] %d %s", UserName, ChatID, Text)
// Ответим пользователю его же сообщением
reply := Text
// Созадаем сообщение
msg := tgbotapi.NewMessage(ChatID, reply)
// и отправляем его
bot.SendMessage(msg)
}
}
}
@0x4380
Copy link

0x4380 commented Jul 4, 2016

under linux arm with go arm build :
looks like there is a problem in telegram-bot-API

`# go build tele.go

command-line-arguments

./tele.go:20: bot.UpdatesChan undefined (type *tgbotapi.BotAPI has no field or method UpdatesChan)
./tele.go:24: bot.Updates undefined (type *tgbotapi.BotAPI has no field or method Updates)
./tele.go:43: bot.SendMessage undefined (type *tgbotapi.BotAPI has no field or method SendMessage)
`

@vovandodev
Copy link

Have the same issue on Mac OS X.

@valsha
Copy link

valsha commented May 17, 2017

.\ go-tg-bot.go:22: bot.UpdatesChan undefined (type *tgbotapi.BotAPI has no field or method UpdatesChan)
.\ go-tg-bot.go:26: bot.Updates undefined (type *tgbotapi.BotAPI has no field or method Updates)
.\ go-tg-bot.go:45: bot.SendMessage undefined (type *tgbotapi.BotAPI has no field or method SendMessage)
same here code is not working

@openprf
Copy link

openprf commented Jun 16, 2017

Simple changes for build below. But it's no tested in work.

// подключаемся к боту с помощью токена
	bot, err := tgbotapi.NewBotAPI("TOKEH")
	if err != nil {
		log.Panic(err)
	}

	bot.Debug = true
	log.Printf("Authorized on account %s", bot.Self.UserName)

	// инициализируем канал, куда будут прилетать обновления от API
	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
	ucfg.Timeout = 60
	upd, _ := bot.GetUpdatesChan(ucfg)
	// читаем обновления из канала
	for {
		select {
		case update := <-upd:
			// Пользователь, который написал боту
			UserName := update.Message.From.UserName

			// ID чата/диалога.
			// Может быть идентификатором как чата с пользователем
			// (тогда он равен UserID) так и публичного чата/канала
			ChatID := update.Message.Chat.ID

			// Текст сообщения
			Text := update.Message.Text

			log.Printf("[%s] %d %s", UserName, ChatID, Text)

			// Ответим пользователю его же сообщением
			reply := Text
			// Созадаем сообщение
			msg := tgbotapi.NewMessage(ChatID, reply)
			// и отправляем его
			bot.Send(msg)
		}

	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment