How to pass parameter to webhook telegram bot PHP - php

Is it possible send other variables with sendMessage method to webhook?
for example setting the todo variable:
function processMessage($message) {
// process incoming message
$message_id = $message['message_id'];
$azione = $message['todo'];
$chat_id = $message['chat']['id'];
$firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : "";
$lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : "";
if (isset($message['text'])) {
$text = $message['text'];
if (strpos($text, "/start") === 0) {
apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Benvenuto '.$firstname.' '.$lastname.' sul BOT di MIMANCHITU, dimmi cosa vuoi fare ['.$azione.']?', 'todo' => "fai qualcosa", 'reply_markup' => array(
'keyboard' => array(array('/consulta', '/guide')),
'one_time_keyboard' => true,
'resize_keyboard' => true)));
}
}

No you can't send any other variable other than what is defined in the documentation.

Related

sendMediaGroup telegram PHP

I am using telegram library
php telegram bot sdk
And I want to send some photos in message using sendMediaGroup
Like this:
$reply = "*photos*";
$telegram->sendMediaGroup([
'chat_id' => $chat,
'media' => [
['type' => 'photo', 'media' => 'attach://photo1' ],
['type' => 'photo', 'media' => 'attach://photo2' ],
],
'photo1' => InputFile::create(file_get_contents("https://".$_SERVER['SERVER_NAME']."/newbot/screens/ctry/en1.jpg")),
'photo2' => InputFile::create(file_get_contents("https://".$_SERVER['SERVER_NAME']."/newbot/screens/ctry/en2.jpg")),
'caption'=> $reply,
'parse_mode' => 'markdown'
]);
I used without json_encode and file_get_contents, but it doesn't worked too
Why do you use that library? Here's the normal solution:
$token = ' TOKEN ';
$website = 'https://api.telegram.org/bot'.$token;
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
$message = $update['message']['text'];
$id = $update['message']['from']['id'];
$name = $update['message']['from']['first_name'];
$surname = $update['message']['from']['last_name'];
$username = $update['message']['from']['username'];
function sendPhoto($id, $photo, $text = null){
GLOBAL $token;
$url = 'https://api.telegram.org/bot'.$token."/sendPhoto?chat_id=$id&photo=$photo&parse_mode=HTML&caption=".urlencode($text);
file_get_contents($url);
}
I also put there some useful variable and the chance to put a caption (don't give that parameter if you don't want one)

how to use callback data in telegram bot

i'm using this library https://github.com/Eleirbag89/TelegramBotPHP to create telegram bot
i want to get user tweeter username like:#username
how to use callback data and then use callback_data value
ex:
bot: username
user: #alex
bot: password
user: 985468
if($text == '🚀 Submit your Detalis'){
$option = array(
array($telegram->buildInlineKeyBoardButton('✅ Done', $url, $callback_data = 'checkIsMember')) );
$keyb = $telegram->buildInlineKeyBoard($option);
$content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => '
Join Our Telegram Channel
<b> Click "✅ Done" to continue </b>
');
$telegram->sendMessage($content);
}
if($text == 'checkIsMember'){
$content = array('chat_id' => $chat_id, 'text' => '
👉 Submit your Twitter username below (Included #)
Ex : #username
');
$telegram->sendMessage($content);
}
Set webhook on file where bot is placed Site must have SSL (https).
https://api.telegram.org/bot(your token)/setWebhook?url=https://example.com/bot_directory/bot.php
Than process data and write bot
$update = json_decode(file_get_contents('php://input'), TRUE);
$botToken = "(your token)";
$botAPI = "https://api.telegram.org/bot" . $botToken;
$msg = $update['message']['text'];
$user_id = $update['message']['from']['id'];
//Simple send button when user send message
$data = http_build_query([
'text' => 'Hi it is my bot',
'chat_id' => $update['message']['from']['id'],
]);
$keyboard = json_encode([
"inline_keyboard" => [
[
[
"text" => "say Hi",
"callback_data" => "say_h"
],
]
],
"resize_keyboard" => true
]);
file_get_contents($botAPI . "/sendMessage?{$data}&reply_markup={$keyboard}");
if (isset($update['callback_query'])) {
if ($update['callback_query']['data'] == 'say_h'){
// your code here when button pressed
}
}

Can't send reply with markup by Telegram Bot API

Trying to build up a simple bot.
Here's my code:
require 'vendor/autoload.php';
use Telegram\Bot\Api;
$telegram = new Api('<MY TOKEN>');
$result = $telegram -> getWebhookUpdates();
$text = $result["message"]["text"];
$chat_id = $result["message"]["chat"]["id"];
$name = $result["message"]["from"]["username"];
$keyboard = [["news"],["more news"],["loolz"]];
if ($text == "/start") {
$reply = "Hello there!";
$reply_markup = $telegram->replyKeyboardMarkup([ 'keyboard' => $keyboard, 'resize_keyboard' => true, 'one_time_keyboard' => false ]);
$telegram->sendMessage([ 'chat_id' => $chat_id, 'text' => $reply, 'reply_markup' => $reply_markup ]);
}
if ($text == "/btn1") {
$reply = "AWESOME! YOU TAPPED 2nd BUTTON :)";
$telegram->sendMessage([ 'chat_id' => $chat_id, 'text' => $reply ]);
}
Condition if ($text == "/btn1") works, but it stops when i add $reply_markup to "btn1" action.
Accordingly, the first condition also does not work due to $reply_markup
This SDK used in my project: click
How should i fix this?
ty <3
In order to use reply markup buttons you should always encode them as a JSON format use json_encode($reply_markup) instead of using $reply_markup.
replace:
$reply_markup = $telegram->replyKeyboardMarkup([ 'keyboard' => $keyboard, 'resize_keyboard' => true, 'one_time_keyboard' => false ]);
to:
$reply_markup = json_encode([ 'keyboard' => $keyboard, 'resize_keyboard' => true, 'one_time_keyboard' => false ]);

How to add a bank account and attach it to a "connected account" on Stripe?

The Stripe API explains how to create an account and update it:
https://stripe.com/docs/api#external_accounts
The Stripe Api also explains how to create a bank account:
https://stripe.com/docs/api#account_create_bank_account
I am trying to create a bank account for a connected account, but unfortunately I keep blocked with this error message:
Missing required param: external_account.
Here is how I proceeded to create the connected account:
public function test_stripe_create_connected_account(Request $request)
{
\Stripe\Stripe::setApiKey("sk_test_...");
$acct = \Stripe\Account::create(array(
"country" => "CH",
"type" => "custom",
"email" => "test#test.ch"
));
}
Then I complete the account by updating it on this way:
public function test_stripe_update_account(Request $request)
{
try {
\Stripe\Stripe::setApiKey("sk_test_...");
$account = \Stripe\Account::retrieve("acct_1BTRCOB5XLEUUY47");
$account->support_email = "victor#krown.ch";
$account->support_phone = "0041764604220";
$account->legal_entity->type = "company";
$account->legal_entity->business_name = "Barbosa Duvanel SARL";
$account->legal_entity->additional_owners = NULL;
//NAME
$account->legal_entity->first_name = "Victor";
$account->legal_entity->last_name = "Duvanel";
//BIRTH DATE
$account->legal_entity->dob->day = 25;
$account->legal_entity->dob->month = 3;
$account->legal_entity->dob->year = 1988;
//ADDRESS
$account->legal_entity->address->city = "Genève";
$account->legal_entity->address->country = "CH";
$account->legal_entity->address->line1 = "Av de la Roseraie 76A";
$account->legal_entity->address->line2 = "Genève";
$account->legal_entity->address->postal_code = "1207";
$account->legal_entity->address->state = "Genève";
//PERSONAL ADDRESS
$account->legal_entity->personal_address->city = "Genève";
$account->legal_entity->personal_address->country = "CH";
$account->legal_entity->personal_address->line1 = "Av de la Roseraie 76A";
$account->legal_entity->personal_address->line2 = "Genève";
$account->legal_entity->personal_address->postal_code = "1207";
$account->legal_entity->personal_address->state = "Genève";
//GENERAL CONDITIONS ACCEPTATION
$account->tos_acceptance->date = time();
$account->tos_acceptance->ip = $_SERVER['REMOTE_ADDR'];
$account->save();
$message = 'OK';
$status = true;
} catch (\Exception $error) {
$message = $error->getMessage();
$status = false;
}
$results = (object)array(
'message' => $message,
'status' => $status,
);
$response = response()->json($results, 200);
return $response;
}
And finally, I am trying to attach a new bank account to my user like that:
public function test_stripe_create_bank_account(Request $request)
{
try {
\Stripe\Stripe::setApiKey("sk_test_...");
$account = \Stripe\Account::retrieve("acct_1BSoOaGS1D3TfSN5");
$account->external_accounts->create(
array(
"object" => "bank_account",
"account_number" => "CH820024024090647501F",
"country" => "CH",
"currency" => "CHF",
)
);
$message = 'OK';
$status = true;
} catch (\Exception $error) {
$message = $error->getMessage();
$status = false;
}
$results = (object)array(
'message' => $message,
'status' => $status,
);
$response = response()->json($results, 200);
return $response;
}
What am I doing wrong?
Any help would be appreciated!
You need to change your external account creation request to wrap the array under the external_account parameter name, like this:
$account->external_accounts->create(array(
"external_account" => array(
"object" => "bank_account",
"account_number" => "CH820024024090647501F",
"country" => "CH",
"currency" => "CHF",
)
));
I am using this below code in my existing project. You can use this code without
any fear
public function Stripe(){
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
try {
// first create bank token
$bankToken = \Stripe\Token::create([
'bank_account' => [
'country' => 'GB',
'currency' => 'GBP',
'account_holder_name' => 'Soura Sankar',
'account_holder_type' => 'individual',
'routing_number' => '108800',
'account_number' => '00012345'
]
]);
// second create stripe account
$stripeAccount = \Stripe\Account::create([
"type" => "custom",
"country" => "GB",
"email" => "<Mail-Id>",
"business_type" => "individual",
"individual" => [
'address' => [
'city' => 'London',
'line1' => '16a, Little London, Milton Keynes, MK19 6HT ',
'postal_code' => 'MK19 6HT',
],
'dob'=>[
"day" => '25',
"month" => '02',
"year" => '1994'
],
"email" => '<Mail-Id>',
"first_name" => 'Soura',
"last_name" => 'Ghosh',
"gender" => 'male',
"phone"=> "<Phone-No>"
]
]);
// third link the bank account with the stripe account
$bankAccount = \Stripe\Account::createExternalAccount(
$stripeAccount->id,['external_account' => $bankToken->id]
);
// Fourth stripe account update for tos acceptance
\Stripe\Account::update(
$stripeAccount->id,[
'tos_acceptance' => [
'date' => time(),
'ip' => $_SERVER['REMOTE_ADDR'] // Assumes you're not using a proxy
],
]
);
$response = ["bankToken"=>$bankToken->id,"stripeAccount"=>$stripeAccount->id,"bankAccount"=>$bankAccount->id];
dd($response);
} catch (\Exception $e) {
dd($e->jsonBody['error']['message']);
}

How to send newly created campaign with mailchimp api v2?

I am using mailchimp api v2. I am using the mailchimp recommended full api v2 php wrapper. I am able to create a campaign, but not sure how to send it. With the send method, It wants the campaign id, but I am letting mailchimp create the campaign id when creating the campaign.
My create campaign code looks like this:
$api_key = "my_api_key";
require('Mailchimp.php');
//Create Campaign
$Mailchimp = new Mailchimp($api_key);
$result = $Mailchimp->campaigns->create('regular',
array('list_id' => 'my_list_id',
'subject' => 'This is a test subject',
'from_email' => 'test#test.com',
'from_name' => 'From Name'),
array('html' => '<div>test html email</div>',
'text' => 'This is plain text.')
);
if( $result === false ) {
// response wasn't even json
echo 'didnt work';
}
else if( isset($result->status) && $result->status == 'error' ) {
echo 'Error info: '.$result->status.', '.$result->code.', '.$result->name.', '.$result->error;
} else {
echo 'worked';
}
This seems to be working for me.
$Mailchimp = new Mailchimp($api_key);
$result = $Mailchimp->campaigns->create('regular',
array('list_id' => 'my_list_id',
'subject' => 'This is a test subjects',
'from_email' => 'test#test.com',
'from_name' => 'From_Name'),
array('html' => '<div>test html email</div>',
'text' => 'This is plain text.')
);
if( $result === false ) {
// response wasn't even json
echo 'sorry';
}
else if( isset($result->status) && $result->status == 'error' ) {
echo 'Error info: '.$result->status.', '.$result->code.', '.$result->name.', '.$result->error;
} else {
echo 'worked';
$mySend = $Mailchimp->campaigns->send($result['id']);
}

Categories