【问题标题】:Check what user send emoji to telegram bot检查哪些用户向电报机器人发送表情符号
【发布时间】:2018-02-24 20:09:39
【问题描述】:

我需要检查用户发送的文字,而不是音频、视频、贴纸或表情符号,因此我尝试创建 if 语句,但我不知道如何检查用户是否向机器人发送表情符号。

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()
}

因此,只有当用户发送带有文字的机器人字符串而不是音频、视频、贴纸或表情符号时,我才需要回复用户。

【问题讨论】:

  • 我不明白你在做什么,但它可能不需要反思。请创建一个minimal reproducible example 来演示该问题。
  • update.Message.Text 是一个字符串,而 Go 是静态类型的,所以你不需要思考它就知道它是否是一个字符串。
  • @JimB 如果用户发送表情符号,机器人会得到这样的字符串“\ud83d\udc93”,这是一个表情符号代码,所以我需要阻止用户发送表情符号
  • 表情符号只是 unicode 字符,它们是完全有效的字符串。其他 unicode 块呢?您想过滤除 ascii 字符之外的任何内容吗?您想过滤基本多语言平面之外的所有内容吗?是否要专门过滤表情符号字符,例如 stackoverflow.com/a/46065932/32880

标签: go telegram telegram-bot


【解决方案1】:

所以我没有找到答案并留下了这个 if 语句:

if reflect.TypeOf(update.Message.Text).Kind() == reflect.String && update.Message.Text != ""

他检查来自用户的消息是否是文本而不是音频、视频、贴纸,但表情符号是文本,以便通过它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2016-10-22
    • 2020-12-15
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    相关资源
    最近更新 更多