Using PHP Telegram Bot InputMediaPhoto Method - php

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']
])
];

Related

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 :-)

how to send access token using CURL PHP

I am new in CURL I need to authenticate user is logged in or not using access token. But how to send x-access-token with post request into header.
I am using. curl library
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();
$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
'newPassword' => $_POST['newPassword'],
'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
$response = array(
'status' => $curl->errorCode,
'message' => $curl->errorMessage,
'response' => $curl->response
);
echo json_encode($response);
} else {
$response = array(
'status' => $curl->httpStatusCode,
'message' => 'Successfylly Login',
'response' => $curl->response
);
echo json_encode($response);
}
You can do this way also.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.abctest.com/abc.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//********************Check these lines for headers*******************
$headers = [
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'x-access-token':'sdsdgdsggrtyrtghf'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//************************ END Headers *********************************
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
You can use setHeader method in Curl class.
RTFM plz.
require __DIR__ . '/vendor/autoload.php';
use \Curl\Curl;
$curl = new Curl();
// check the following line
$curl->setHeader('x-access-token', 'YOUR-TOKEN-HERE');
$curl->post('http://localhost:3011/user/reset-password',array('x-access-token'=>$user['token']), array(
'newPassword' => $_POST['newPassword'],
'confirmPassword' => $_POST['confirmPassword']
));
if ($curl->error) {
$response = array(
'status' => $curl->errorCode,
'message' => $curl->errorMessage,
'response' => $curl->response
);
echo json_encode($response);
} else {
$response = array(
'status' => $curl->httpStatusCode,
'message' => 'Successfylly Login',
'response' => $curl->response
);
echo json_encode($response);
}

PHP send slack messages with attachments and variable values

Trying to send messages to slackchannels with incoming webhooks installed on them. Attachments needs to be sent along with the message and PHP variables holds these URLs. Similary, I want to send some ID's which are again hold in some PHP variables. Here is my server side PHP code:
<?php
$testplan_name = $_POST[plan]; //test plan name coming from the client
$url1 = $_POST[run_url]; //run url coming from the client
$url2 = $_POST[plan_url]; //plan url coming from the client
$room = "random";
$icon_url = ":ghost:";
$username = "Test";
$attachments = array([
'fallback' => 'Hey! See this message',
'pretext' => 'Here is the plan name ${testplan_name}',
'color' => '#ff6600',
'fields' => array(
[
'title' => 'Run URL',
'value' => 'url1',
'short' => true
],
[
'title' => 'Build URL',
'value' => 'url2',
'short' => true
]
)
]);
$data = "payload=" . json_encode(array(
"channel" => "#{$room}",
"icon_emoji" => $icon_url,
"username" => $username,
"attachments" => $attachments
));
$url = "https://hooks.slack.com/services/XXXX/XXX/XXXXXXXXXXXXX"; //got from slack as a webhook URL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
echo var_dump($result);
if($result === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
If you see in the attachments variable above, there is variable inside of pretext which tries to print the value of ${testplan_name} declared at the top. However, it does not seem to work and the program is failed to post messages to slack channels. Similarly, I want to print values of url1 and url2 in the attachments -> fields values as can be seen above(the way I am trying to print). The program just works fine if I do not try to use any variables and get their values while posting messages. How do I print the values of these variables in messages?
(slack is a messaging platform for teams, if you don't know)
Try this instead>
$attachments = array([
'fallback' => 'Hey! See this message',
'pretext' => 'Here is the plan name '.$testplan_name,
'color' => '#ff6600',
'fields' => array(
[
'title' => 'Run URL',
'value' => $url1,
'short' => true
],
[
'title' => 'Build URL',
'value' => $url2,
'short' => true
]
)
]);

Mailchimp Does not work on production [laravel on azure]

I deployed a small laravel app that subscribes a user to a mailchimp list.
it's very basic yet it does not work on production
NOTE: EVERYTHING is fine in Localhost env and a CONTACT FORM WORKS FINE(uses SMTP)
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=AUSERNAME
MAIL_PASSWORD=APASSWORD
MAIL_ENCRYPTION=TLS
MAILCHIMP_APIKEY=APIKEYHERE
MAILCHIMP_LIST_ID=LISTIDHERE
Controller
Newsletter::subscribe($request->email, [
'firstName' => 'test',
'lastName' => 'tessst',
'listName' => 'whishlist' ], 'subscribers');
return response()->json([
'status' => 'success',
'msg' => 'Subscribed successfully']);
laravel-newsletter Config file
<?php
return [
'apiKey' => env('MAILCHIMP_APIKEY'),
'defaultListName' => 'subscribers',
'lists' => [
'subscribers' => [
'id' => '5920168294',
],
'whishlist' => [
'id' => '8e553f3d39',
],
],
];
My i guess is that this has something to do with HTTPS (i fixed the issue by adding a file cacert.pem and referencing it in php.ini )
if this is the issue how can i fix this on azure?
And sorry there is no error output since it returns success to the ajax call.(if how can i get the response from mailchimp to check the error?)
Thanks in advance.
Well i dont know what is the issue here.
but i managed to make this work when i got rid of the package i'm using which is spatie/laravel-newsletter and used CURL instead and API V3.
$email = $request->email;
$listid = env('MAILCHIMP_LIST_ID');
$apikey = env('MAILCHIMP_APIKEY');
$server = substr($apikey, strpos($apikey, '-') + 1);
$auth = base64_encode('user:' . $apikey);
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'test1',
'LNAME' => 'test2',
),
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://'. $server.'api.mailchimp.com/3.0/lists/'. $listid .'/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
curl_close($ch);
return $result;

Telegram Bot custom keyboard in 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

Categories