做一個 telegram bot (復讀機)

<article id="content" class="article-content" tabindex="0" style="outline-style: none;">

嘗試

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

獲得 telegram bot token

和 BotFather 交談即可

中途需要設置一下名字和查找路徑

image.png

go get

首先獲取 api 包

<pre class="cm-s-default" style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">go get -u github.com/go-telegram-bot-api/telegram-bot-api</pre>

code

<pre class="cm-s-default" style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">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)
}

}
}</pre>

Run

注意 終端需要能訪問 telegram 的 API

image.png

效果

image.png

</article>

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容