英文:
how to send userleft message for telegram in golang
问题
我正在使用 Go 编程语言编写一个 Telegram 机器人。当用户加入或离开时,我该如何发送一条消息呢?
我尝试了以下代码:
if message.UserJoined == true {
bot.SendMessage(message.Chat, "", nil)
}
但是它并没有起作用,我得到了以下错误信息:
./main.go:291: invalid operation: message.UserJoined == true (mismatched types telebot.User and bool)
英文:
I'm writing a telegram bot with golang programming language. How can i send a message when a user left or join?
I try this
if (message.UserJoined == true){
bot.SendMessage(message.Chat, "" ,nil)
}
but it does not work and I get this error
./main.go:291: invalid operation: message.UserJoined == true (mismatched types telebot.User and bool)
答案1
得分: 1
UserJoined 是一个定义的 User 结构体:
// UserJoined 可能是机器人本身。
UserJoined User `json:"new_chat_participant"`
它不匹配一个布尔值。
没有看到代码的其余部分,我无法确定你正在测试消息的内容。
我认为你需要的是像这样的东西:
if message.UserJoined.ID > 0 {
// 做一些事情
}
英文:
UserJoined is a User Struct defined:
// UserJoined might be the Bot itself.
UserJoined User `json:"new_chat_participant"`
It does not match a bool
Without seeing the rest of the code, I cant tell what you are testing the message for.
I beileve what your needing is something like
if message.UserJoined.ID > 0 {
//Dosomething
}
答案2
得分: 0
在这种情况下,由于你试图比较两种不同的类型,所以出现了错误。然而,我不确定你正在使用哪个API,或者你是否在使用自己的API,所以我不确定message.UserJoined应该表示什么。不过,根据你的使用情况,我认为你应该将其与nil进行比较。即使用 if message.UserJoined != nil {}。
英文:
In this case because you are trying to compare two different types it is failing. However, I'm not sure which API you are using or if you are using your own so I'm not sure what message.UserJoined is supposed to represent. However I would think based on your apparent use case that you should be comparing it to nil instead. i.e. use if message.UserJoined != nil {}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论