我需要检查用户发送的是什么,而不是音频,视频,贴纸或表情符号,所以我尝试创建if语句,但我不知道如何检查用户是否将表情符号发送给bot。
if reflect.TypeOf(update.Message.Text).Kind() == reflect.String && update.Message.Text != "" //Check if message from user is not emoji {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "It's a Text")
bot.Send(msg)
} else {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "It's a not Text")
bot.Send(msg)
}
完整代码:
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"reflect"
"runtime"
"strings"
"github.com/Syfaro/telegram-bot-api"
)
func telegramBot() {
//Create bot
bot, err := tgbotapi.NewBotAPI("TOKEN")
if err != nil {
log.Panic(err)
}
//Check if bot autorized
bot.Debug = true
log.Printf("Autorized on account %s", bot.Self.UserName)
//Set update timeout
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
//Get updates from bot
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
if reflect.TypeOf(update.Message.Text).Kind() == reflect.String && update.Message.Text != "" {
//Create search url
url := update.Message.Text
request := "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + url + "&limit=1&origin=*&format=json"
//Check from who was arrived message and send to him answer from wikipedia
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Message to user")
//Send message
bot.Send(msg)
} else {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "It's not a Text")
//Send message
bot.Send(msg)
}
}
}
func main() {
//Set maximum cpu cores to use
num := runtime.NumCPU()
runtime.GOMAXPROCS(num)
//Call Bot
telegramBot()
}
因此,只有当他发送带字的僵尸字符串,而不是音频,视频,贴纸或表情符号时,我才需要回复用户。
答案 0 :(得分:0)
所以我找不到答案,留下了这句话:
if reflect.TypeOf(update.Message.Text).Kind() == reflect.String && update.Message.Text != ""
他检查来自用户的信息是否是文字而不是音频,视频,贴纸,但表情符号是文字,所以传递它。