Answer the question
In order to leave comments, you need to log in
Trouble with php code for Telegram bot?
Hello! Teaching a bot to speak! There are moments that don't work.
Here is a piece of code that works perfectly:
$hello = 'привет';
$bot_hello = strripos($message, $hello);
if ($bot_hello === false) {
} else {
sendMsg($group_id, 'Привет, '. $first_name . '!' );
}
function strtolower_ru($message) {
$alfavitlover = array('ё','й','ц','у','к','е','н','г', 'ш','щ','з','х','ъ','ф','ы','в', 'а','п','р','о','л','д','ж','э', 'я','ч','с','м','и','т','ь','б','ю');
$alfavitupper = array('Ё','Й','Ц','У','К','Е','Н','Г', 'Ш','Щ','З','Х','Ъ','Ф','Ы','В', 'А','П','Р','О','Л','Д','Ж','Э', 'Я','Ч','С','М','И','Т','Ь','Б','Ю');
return str_replace($alfavitupper,$alfavitlover,strtolower($message));
}
$hello = array();
$hello[] = 'привет';
$hello[] = 'хай';
$hello[] = 'здравствуйте';
$hello[] = 'ку';
Answer the question
In order to leave comments, you need to log in
1. php.net/manual/ru/function.mb-strtolower.php
2. php.net/manual/ru/function.array-rand.php
If you want to make a real talking bot, then simply coding the conditions is not enough. It is necessary to use machine learning methods, which for the most part involve converting text into a more machine-understandable language, for example, N-grams or the same soundex ..
If this is a regular bot, then why not do like everyone else - commands in Latin with a slash at the beginning.
Regarding the problems that you encountered: you need to work with Cyrillic using the mb_*** functions (you can, of course, turn on mb_func_overload and not worry, but it's better to explicitly use multibyte functions).
Choosing a random element from an array - well, damn it, there is the array_rand () function, but even without it, one could guess to take a random number from zero to the size of the array and use it as a key.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question