用 Go 語言實現一個 telegram 的 bot - 成功復讀

telegram bot(復讀機)

嘗試

用 Go 語言做了一個 telegram 的 bot . 用來簡單實現對話(復讀)。

獲得 telegram bot token

和 BotFather 交談即可
中途需要設置一下名字和查找路徑
image.png

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
image.png

效果

image.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章