J
J
JohnnyParazit2017-08-31 11:32:01
PHP
JohnnyParazit, 2017-08-31 11:32:01

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 . '!' );
}

The first moment that does not work is the case of letters, how to defeat it? If you write "Hello" to the bot, then he will no longer understand me! Tried like this:
function strtolower_ru($message) {

$alfavitlover = array('ё','й','ц','у','к','е','н','г', 'ш','щ','з','х','ъ','ф','ы','в', 'а','п','р','о','л','д','ж','э', 'я','ч','с','м','и','т','ь','б','ю');

$alfavitupper = array('Ё','Й','Ц','У','К','Е','Н','Г', 'Ш','Щ','З','Х','Ъ','Ф','Ы','В', 'А','П','Р','О','Л','Д','Ж','Э', 'Я','Ч','С','М','И','Т','Ь','Б','Ю');

return str_replace($alfavitupper,$alfavitlover,strtolower($message));

}

But it doesn't come out! The second point is arrays. How to make it so that there are several options for messages to which it reacts, for example:
$hello = array();
  $hello[] = 'привет';
  $hello[] = 'хай';
  $hello[] = 'здравствуйте';
  $hello[] = 'ку';

And a random answer from the bot array!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2017-08-31
@BorisKorobkov

1. php.net/manual/ru/function.mb-strtolower.php
2. php.net/manual/ru/function.array-rand.php

I
Ivan Koryukov, 2017-08-31
@MadridianFox

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 question

Ask a Question

731 491 924 answers to any question