-
-
Save zaz600/d477902cce578a815379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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