• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Managing CallbackQuery and message types in Telegram bots

  • Автор темы Автор темы Lomanu4
  • Дата начала Дата начала

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Continuing from the

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

, this time I will describe the changes I made to the library.

The user in the Telegram bot can send different types of messages, such as text messages, photos, videos, etc. In some cases, we need to know what type of message the client has sent so that we can respond to it accordingly, or perhaps we need to limit the type of message for the client

So, a method had to be created to detect the type of message sent by the client, and as a result, the message type could be received using the following method:


$type = Bot::message()->type();
I think this is a clean and simple syntax, don't you?

The values it returns are one of these:
'text', 'photo', 'video', 'audio', 'document', 'sticker', 'animation', 'location', 'contact', 'poll'

I also put the consts of these types in the message method, which are:


const TYPE_TEXT = 'text';
const TYPE_PHOTO = 'photo';
const TYPE_VIDEO = 'video';
const TYPE_AUDIO = 'audio';
const TYPE_DOCUMENT = 'document';
const TYPE_STICKER = 'sticker';
const TYPE_ANIMATION = 'animation';
const TYPE_LOCATION = 'location';
const TYPE_CONTACT = 'contact';
const TYPE_POLL = 'poll';
const TYPE_MESSAGE = 'message';

These constants are accessible from the Message class, so creating a condition to check the type of user message could look something like this:


if(Bot::message()->type() == Message::TYPE_TEXT){
// Do something.
}
else{
Bot::new()->text('Please send your message as text.')->send();
}
What is your opinion about this structure? I would be happy if you share it with me.
Detecting a query callback in a Telegram bot



Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



There is another type of message. When the client clicks on an inline button In this case, Telegram sends us a Callback that also contains the data object of that message.

I created the isCallbackQuery method for this purpose, which can be used as follows:


if ( Bot::isCallbackQuery() ) {

$data = Bot::callback()->data();

Bot::new()->text("? Get Callback Query:\nData : $data")->send();
}

So if the value of Bot::isCallbackQuery() is true, we know that the client has clicked a button. In this case, we have access to the callback method and we can get the data of the button that the client clicked using Bot::callback()->data()

View on GitHub:

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



This library is under development and is not yet ready for use.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу