Created
September 18, 2019 10:23
-
-
Save dragonfly-net/95e476a29edf3950ca5dc9a6b9cade91 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
// Forked from https://gist.github.com/zaz600/b377834ef863de563bb1 | |
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 := <-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