Comments (3)
Via \TelegramBot\Api\Client::on
you can catch any messages from users and react to them:
/**
* Use this method to add an event.
* If second will return true (or if you are passed null instead of closure), first one will be executed.
*
* @param \Closure $event
* @param \Closure|null $checker
*
* @return \TelegramBot\Api\Client
*/
public function on(Closure $event, Closure $checker = null)
{
$this->events->add($event, $checker);
return $this;
}
For example, to process each message, you can do:
$bot->on(function($message) use ($bot){
file_put_contents("1.txt", print_r($message,true));
}, function($message) use ($name){
return true;
});
For processing command $name
via $action
closure you can use \TelegramBot\Api\Client::command
or own code:
$bot->on(function (Message $message) use ($action) {
preg_match('/^\/([^\s@]+)(@\S+)?\s?(.*)$/', $message->getText(), $matches);
if (isset($matches[3]) && !empty($matches[3])) {
$parameters = str_getcsv($matches[3], chr(32));
} else {
$parameters = [];
}
array_unshift($parameters, $message);
$action = new ReflectionFunction($action);
if (count($parameters) >= $action->getNumberOfRequiredParameters()) {
$action->invokeArgs($parameters);
}
return false;
},
function (Message $message) use ($name) {
if (!strlen($message->getText())) {
return false;
}
preg_match('/^\/([^\s@]+)(@\S+)?\s?(.*)$/', $message->getText(), $matches);
return !empty($matches) && $matches[1] == $name;
}
);
from api.
Thanks for your detailed answer!
from api.
> $bot->on(function($message) use ($bot){
> file_put_contents("1.txt", print_r($message,true));
> }, function($message) use ($name){
> return true;
> });
the type of the $message is not the Message , it's Update , if you want to process incoming messages your code should be something like this :
$bot->on(function(Update $update) use ($bot){
$message = $update->getMessage();
$content = $message->getText();
file_put_contents('debug.txt',$content);
}, function($message){
return true;
});
from api.
Related Issues (20)
- No description provided.
- how to use ForceReply HOT 2
- how to send message with entities ?
- 悬赏求定制telegrambot
- Внезапно перестали работать БОТы HOT 2
- Как получить ID отправленного сообщения и ID чата HOT 8
- Need up setting up HOT 5
- delete_message
- Support BOT API 7.0 (december 29 2023) HOT 2
- Support BOT API 7.1 (february 16 2024) HOT 2
- Support BOT API 7.2 (march 31, 2024)
- Add a Compatibility Table HOT 1
- composer require telegram-bot/api
- Support Bot API 7.3 (may 6 2024)
- @lav095
- Support Bot API 7.4 (may 28, 2024)
- Support Bot API 7.5 (june 18, 2024)
- Hello
- Support Bot API 7.6 (july 1, 2024)
- https://aggennn-inndahh-gaaass-perrtaminnna.wixzey.com/
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from api.