Skip to content

Instantly share code, notes, and snippets.

@zaz600
Last active March 10, 2022 20:36
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/d477902cce578a815379 to your computer and use it in GitHub Desktop.
Save zaz600/d477902cce578a815379 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/Syfaro/telegram-bot-api"
"log"
"fmt"
)
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
UserID := update.Message.From.ID
// ID чата/диалога.
// Может быть идентификатором как чата с пользователем
// (тогда он равен UserID) так и публичного чата/канала
ChatID := update.Message.Chat.ID
// Текст сообщения
Text := update.Message.Text
log.Printf("[%s] %d %d %s", UserName, UserID, ChatID, Text)
var reply string
if update.Message.NewChatParticipant.UserName != "" {
// В чат вошел новый пользователь
// Поприветствуем его
reply = fmt.Sprintf(`Привет @%s! Я тут слежу за порядком. Веди себя хорошо.`,
update.Message.NewChatParticipant.UserName)
}
if reply != "" {
// Созадаем сообщение
msg := tgbotapi.NewMessage(ChatID, reply)
// и отправляем его
bot.SendMessage(msg)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment