分享
用 Go 语言实现一个 telegram 的 bot - 成功复读
yhyddr · · 1644 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
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
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1644 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
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