Skip to content

Fire and Forget

Kyle2142 edited this page Jun 4, 2019 · 1 revision

Often we want to do something like "Mybot is typing..." (sendChatAction).
We don't really care if this throws an error, and we certainly don't care about the return value

Generally to ignore the error/result you would use:

try {
    $bot->api->sendChatAction(['chat_id' => $chat_id, 'action' => 'typing']);
} catch(Exception $e) {
}

This becomes really messy and takes up 4 lines of code.
Hence I introduced a new method, which albeit not as "nice" looking regarding OOP and whatever, does the job well and makes your code cleaner:

$bot->fireAndForget('sendChatAction', ['chat_id' => $chat_id, 'action' => 'typing']);

For the curious: This function really doesn't do any magic. It just catches TelegramException and RuntimeException (which can be thrown by $bot->api->method)