做一個 telegram bot (復讀機)

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

嘗試

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

獲得 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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容