用-Go-语言实现一个-telegram-的-bot-成功复读

8次阅读

共计 752 个字符,预计需要花费 2 分钟才能阅读完成。

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

效果

正文完
 0