telegram bot(復讀機)
嘗試
用 Go 語言做了一個 telegram 的 bot . 用來簡單實現對話(復讀)。
獲得 telegram bot token
和 BotFather 交談即可
中途需要設置一下名字和查找路徑
go get
首先獲取 api 包
go get -u github.com/go-telegram-bot-api/telegram-bot-api
code
package main
import (
"log"
"os"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyToMessageID = update.Message.MessageID
if _, err := bot.Send(msg); err != nil {
og.Panic(err)
}
}
}
Run
注意 終端需要能訪問 telegram 的 API
效果