I’m working on a Telegram bot using PHP and webhooks. I want to know if there’s a way to include additional custom parameters when using the sendMessage API method.
Specifically, I need to pass a custom variable like ‘action’ along with the standard message data. Here’s what I’m trying to achieve:
function handleIncomingMessage($data) {
// handle the received message
$msg_id = $data['message_id'];
$task = $data['action'];
$user_chat = $data['chat']['id'];
$user_first = isset($data['chat']['first_name']) ? $data['chat']['first_name'] : "";
$user_last = isset($data['chat']['last_name']) ? $data['chat']['last_name'] : "";
if (isset($data['text'])) {
$user_text = $data['text'];
if (strpos($user_text, "/begin") === 0) {
sendTelegramRequest("sendMessage", array(
'chat_id' => $user_chat,
"text" => 'Hello '.$user_first.' '.$user_last.' welcome to our bot! What would you like to do ['.$task.']?',
'action' => "do something",
'reply_markup' => array(
'keyboard' => array(array('/check', '/help')),
'one_time_keyboard' => true,
'resize_keyboard' => true
)
));
}
}
}
Is this approach correct or is there another way to pass custom variables through the webhook?
The Telegram Bot API ignores custom parameters like ‘action’ in sendMessage - they just won’t work. You’ll need to store that custom data somewhere else, like a database tied to the chat_id and message_id. When users hit your bot, you can pull that context data to figure out what happens next. For inline keyboards, just encode the action directly in callback_data or stick it in the message text. If you’re dealing with complex workflows, a state machine works great for tracking where users are in the process.
unfortunately, telegram’s api doesn’t allow custom params like ‘action’ in sendMessage. i usually store that info in sessions or temp files using chat_id. when the webhook triggers, i just check that data for context. it’s been working fine for my bots!
That won’t work - Telegram’s API just drops any non-standard parameters. Hit this same problem about a year ago building a workflow bot. I ended up using Redis for simple state management, but any database works (even file storage for low traffic). Store your custom data with a key like “chat_{$chat_id}_state” before sending the message, then grab it when processing the webhook. For complex stuff, encode the action right into callback_data for inline keyboards - you get 64 bytes to play with. This approach’s been rock solid for multi-step conversations and tracking user context.