NovaGram

An Object-Oriented PHP library for Telegram Bots

View the Project on GitHub skrtdev/NovaGram

Create a Bot Instance

use skrtdev\NovaGram\Bot;
new Bot(string $token, array $settings = [], $logger = null);

Token

$token is the Bot’s token generated by @BotFather

Settings

$settings is an array which contains some NovaGram configurations.

None of these fields is required

key type default description
username string no Bot username, recommended to pass only if using command handlers on webhook
mode integer auto Mode for update handling (or no handling at all)
json_payload boolean true Whether or not print json payload
log_updates integer no Chat id where raw json updates will be sent (set to false to disable)
debug integer no Chat id where debug logs will be sent if an api error occurs (set to false to disable)
async bool true Whether or not process updates concurrently
command_prefixes array ['/'] Characters for commands prefixes. e.g. /start, .info
bot_api_url string https://api.telegram.org Url for custom bot api
restart_on_changes bool false Auto restart when Bot file is edited
workers int cores count * 10 Max amount of processes that will run simultaneously (CLI only)
group_handlers bool true Whether to execute all the handlers of an update in the same process (true), or fork a process for each handler (false)
wait_handlers bool false Whether to wait for handlers to finish when closing script
skip_old_updates bool false Whether to not process updates sent before starting the bot
threshold int null Defaults to 10 when using getUpdates. Amount of max seconds the script will wait instead of throwing a TooManyRequestsException
logger int Monolog\Logger::INFO Monolog\Logger constant for logging
disable_ip_check bool false Whether or not disable Telegram IP check (could be useful in case of reverse proxy, such as ngrok)
export_commands bool true Whether to call exportCommands when idling on CLI
include_classes bool false Whether to automatically include and fire Commands Class Handlers (include all files that ends with Command.php inside the main script directory)
exceptions bool true Whether or not throw \skrtdev\Telegram\Exception(s) when API Errors occurs
database array null Database array connection info or instance of an existing PDO database (novagram will be used as prefix)
parse_mode string null Default parse_mode for methods that require it
disable_web_page_preview bool null Default disable_web_page_preview for methods that require it
disable_notification bool null Default disable_notification for methods that require it
allow_sending_without_reply bool null Default allow_sending_without_reply for methods that require it
only_if_banned bool null Default only_if_banned for unbanChatMember method
disable_auto_webhook_set bool false Whether to not set webhook automatically when opening script url if a webhook is not set
use_preg_match_instead_of_preg_match_all bool false Whether to use preg_match instead of preg_match_all for retrieving matches in command handlers such as onCommand and onCallbackData. (Added for v2 compatibility)

Mode

Can be either Bot::WEBHOOK, Bot::CLI or Bot::NONE. Normally it is automatic, and you can not specify it at all

JSON Payload

JSON Payload is a different way to make API Calls. In Webhooks, you can print a JSON-Encoded request directly in the webhook page, that will be processed by Telegram afterwards, without effectively making an API Call.

Only One API Call can use JSON Payload simultaneously

Pro:

Cons:

How to make a JSON Payload API Call

Examples

use skrtdev\NovaGram\Bot;
$Bot = new Bot("YOUR_TOKEN", [
    "json_payload" => true,
    "debug" => 634408248,
    "parse_mode" => "HTML",
    "exceptions" => false
]);

In this example, json_payload is enabled, debug will be sent to 634408248, default parse_mode is HTML, and no \skrdtev\Telegram\Exception(s) will be thrown when an API Error occurs

Go to NovaGram Objects