Telegram Bot custom keyboard in PHP - php

I'm trying to make a Telegram Bot in PHP with a custom keyboard. The message is delivered, but the custom keyboard won't work. $keyb = array('keyboard' => array(array("A", "B"))); also no succes.
The sendMessage method referrers to ReplyKeyboardMarkup for the object. Making an array for ReplyKeyboardMarkup doesn't work. Also tried to json_encode($keyb) but that's also not the solution.
I searched in GitHub for examples but I haven't found one where the custom keyboard is used. Telegram runs on iPhone and desktop, both up-to-date.
Sample code:
$url = "https://api.telegram.org/bot<token>/sendMessage";
$keyb = array('ReplyKeyboardMarkup' => array('keyboard' => array(array("A", "B"))));
$content = array('chat_id' => <chat_id>, 'reply_markup' => $keyb, 'text' => "Test");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //fix http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);

The docs seem to indicate you need to provide the reply_markup parameter as a JSON serialised object... kinda stupid for a form POST endpoint:
$replyMarkup = array(
'keyboard' => array(
array("A", "B")
)
);
$encodedMarkup = json_encode($replyMarkup);
$content = array(
'chat_id' => <chat_id>,
'reply_markup' => $encodedMarkup,
'text' => "Test"
);
Does this one work?

$keyboard = array(array("[Destaques]","[Campinas e RMC]","[esportes]"));
$resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
$reply = json_encode($resp);
$url = $GLOBALS[website]."/sendmessage?chat_id=".$chatId."&text=oi&reply_markup=".$reply;
file_get_contents($url);
This code works fine!

$keyboard = [
'keyboard' => [
[
['text' => 'hourly', 'callback_data' => 'Every hour'],
['text' => 'daily', 'callback_data' => 'Every day'],
['text' => 'weekly', 'callback_data' => 'Every week'],
['text' => 'monthly', 'callback_data' => 'Every month'],
['text' => 'yearly', 'callback_data' => 'Every year'],
] ]
];
this works for me

Related

PHP CURL Open Ai Remove The Quotes From Post Data

Below is my PHP Curl script that works to post to Open AI, this is all working as it should.
I want to be able to set the values from posted data like this.
$getOpenAITemperature = $_POST[OpenAITemperature];
$getmaxtokens = $_POST[OpenAIMaximumLength];
$getTopp = $_POST[OpenAITopP];`
But when I do it added quotes to the posted values and it stops working.
Like this.
$postData = [
'model' => $getOpenAIModel,
'prompt' => $getRequest,
'temperature' => "0.24",
'max_tokens => "250",
'top_p' => "1",
But it needs to look like this to work.
$postData = [
'model' => $getOpenAIModel,
'prompt' => $getRequest,
'temperature' => 0.24,
'max_tokens => 250,
'top_p' => 1,
How can I remove the quotes around the numbers ? The quotes around the model and prompt are fine its just the numbers.
*** The script below here works fine ***
$getOpenAITemperature = 0.5;
$getmax_tokens = 250;
$gettop_p = 1;
$OPENAI_API_KEY = "sk-123";
$getOpenAIModel = "text-davinci-003";
$getRequest "My Question";
$ch = curl_init();
$headers = [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$OPENAI_API_KEY.''
];
$postData = [
'model' => $getOpenAIModel,
'prompt' => $getRequest,
'temperature' => $getOpenAITemperature,
'max_tokens' => $getTopp,
'top_p' => $getmaxtokens,
'best_of' => 2,
'frequency_penalty' => 0.0,
'presence_penalty' => 0.0,
'stop' => '["\n"]',
];
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$result = curl_exec($ch);`
I have tried a number of things like php trim() and str_replace but nothing worked.
You can cast the strings to an int or a float like so:
$postData['temperature'] = (float) $postData['temperature'];
$postData['max_tokens'] = (int) $postData['max_tokens'];
Check the PHP docs for type casting
https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

paygate payweb3 integration in php website

I am trying to integrate paygate payment gateway integration into my PHP API. I have gone through their documentation but it is not clear for me. There were three endpoints
initiate
return
query
I don't which endpoint to use. Now I am using initiate endpoint, but when testing in postman I am getting an error
ERROR=DATA_CHK error
Can anyone guide me to sort this out...
public function subscription(Request $request) {
$validator = Validator::make(
$request->all(),
array(
'subscription' => 'required|integer',
'email' => 'required|exists:users,email',
'amount' => 'required|integer',
'payment_mode' => 'required|integer',
),
array(
'exists' => 'The :attribute doesn\'t exists',
)
);
if ($validator->fails()) {
$error_messages = implode(',', $validator->messages()->all());
$response_array = array('success' => false, 'error' => Helper::get_error_message(101), 'error_code' => 101, 'error_messages'=>$error_messages);
} else {
$encryptionKey = 'AQ2Uted8BidhkIEv6oYgrqwS';
$data = array(
'PAYGATE_ID' => 1039433100014,
'REFERENCE' => 'pgtest_123456789',
'AMOUNT' => 3299,
'CURRENCY' => 'ZAR',
'RETURN_URL' => 'http://localhost/avvata/public/home',
'TRANSACTION_DATE' => date('Y-m-d H:i:s'),
'LOCALE' => 'en-za',
'COUNTRY' => 'ZAF',
'EMAIL' => $request->email,
);
$checksum = md5(implode('', $data) . $encryptionKey);
$data['CHECKSUM'] = $checksum;
$fieldsString = http_build_query($data);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, 'https://secure.paygate.co.za/payweb3/initiate.trans');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsString);
//execute post
$result = curl_exec($ch);
echo $result;
exit();
//close connection
curl_close($ch);
}`
Thanks in advance :-)

Using PHP Telegram Bot InputMediaPhoto Method

I was trying to send some images into one message with my Telegram bot. I used InputMediaPhoto method to send, but unfortunately doesn't work.
Here is my code:
$url = "https://api.telegram.org/bot" . "TOKEN" . "/InputMediaPhoto";
$postContent = [
'chat_id' => $GLOBALS['chatId'],
'media' => [
['type'=>'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'], //Just for test
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg']
]
];
post($url, $postContent);
function post($url, $postContent)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postContent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
There are two problems.
The medthod you need to use is called sendMediaGroup. Also the media you are sending must be in a JSON-serialized array.
Changing your code to this should work:
$url = "https://api.telegram.org/bot" . "TOKEN" . "/sendMediaGroup";
$postContent = [
'chat_id' => $GLOBALS['chatId'],
'media' => json_encode([
['type'=>'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg'],
['type' => 'photo' ,'media' => 'http://www.alcan5000.com/JPG/64Caliente.jpg']
])
];

cURL Redirect Returned URL

I want to send data from site A to site B. I've successfully transferred data but I want to redirect the url to the returned url generated from Curl. Below is my code.
$jsonData = array(
'first_name' => "$fname",
'last_name' => "$lname",
'phone_number' => "$phone",
'gender' => "$gender",
'email' => "$email",
'businessname' => "$businessname",
'natureofbusiness' => "$biznature",
'address' => "$address",
'utm' => "esg",
'date' => "$d"
);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
This is the result
{"data":{"success":true,"redirectUrl":"https://url.com/authorize
/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGlt
YSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-
U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}
I need help to redirect to that Url.
Convert JSON result into array
$result = '{"data":{"success":true,"redirectUrl":"https://url.com/authorize/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGltYSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}';
$result = json_decode($result, true);
Now get url from array.
if(isset($result["data"])){
if($result["data"]["success"]==true){
$url = $result["data"]["redirectUrl"];
header("location:".$url);
}
}

You must log in before using this part of Bugzilla, code:410

I am able to get the GET request working but having issues related to authentication in POST and PUT request. I am getting the error "You must log in before using this part of Bugzilla". I have provided the correct username and password. I have tried CURLAUTH_ANY as well as CURLAUTH_BASIC. I have tried both PUT and POST request. Any help is appreciated.
$url ="http://localhost:8080/bugzilla/rest/bug/2";
$apikey = "IZC4rs2gstCal0jEZosFjDBRV9AQv2gF0udh4hgq";
$data = array(
"product" => "TestProduct",
"component" => "TestComponent",
"version" => "unspecified",
"summary" => "This is a test bug - please disregard",
"alias" => "SomeAlias",
"op_sys" => "All",
"priority" => "P1",
"rep_platform" => "All"
);
$str_data = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Accept: application/json"));
$username = 'ashish.sureka#in.abb.com';
$password = 'abbincrc';
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $result
Following code solved my problem. I have written a blog on it which might be useful to others encountering the same problem.
<?php
$url = 'http://localhost:8080//bugzilla/xmlrpc.cgi';
$ch = curl_init();
$header = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array( 'Content-Type: text/xml', 'charset=utf-8' )
);
curl_setopt_array($ch, $header);
$bugreport = array(
'login' => 'ashish.sureka#in.abb.com',
'password' => 'abbincrc',
'product' => "TestProduct",
'component' => "TestComponent",
'summary' => "Bug Title : A One Line Summary",
'assigned_to' => "ashish.sureka#in.abb.com",
'version' => "unspecified",
'description' => "Bug Description : A Detailed Problem Description",
'op_sys' => "All",
'platform' => "All",
'priority' => "Normal",
'severity' => "Trivial"
);
$request = xmlrpc_encode_request("Bug.create", $bugreport);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_exec($ch)
?>

Categories