0

I need to check what user send a word, not a audio, video, sticker or emoji, so I try to create if statement but I don't know how to check if user send emoji to 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)
}

Full code:

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

So I need to response user only if he send bot string with word, not an audio, video, sticker or emoji.

Blauharley
  • 4,186
  • 6
  • 28
  • 47
  • I don't understand what you're doing, but it probably doesn't need reflection. Please create a [mcve] demonstrating the issue. – JimB Jan 30 '18 at 19:23
  • `update.Message.Text` is a string, and Go is statically typed, so you don't need to reflect on it to know if it's a string. – JimB Jan 30 '18 at 20:02
  • @JimB if user send emoji, bot get string like this "\ud83d\udc93", it's a emoji code, so I need to prevent user to send emoji –  Jan 30 '18 at 20:09
  • 3
    Emoji's are just unicode characters, which are perfectly valid strings. What about the other unicode blocks? Do you want to filter anything outside of ascii characters? Do you want to filter everything outside the Basic Multilingual Plane? Do you want to specifically filter just emoji characters, like https://stackoverflow.com/a/46065932/32880? – JimB Jan 30 '18 at 20:24

1 Answers1

0

So I don't find answer and left this if statement:

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

He check if message from user is text not a audio, video, sticker but emoji is text so thay pass it.