How to use Telegram bot api to send messages after blocking? - telegram

I'm create a telegram bot with https://github.com/php-telegram-bot/core/. Bot is located in Russia. In some reason, Russian authority block some Telegram features and my bot can't send messages to registered users. To avoid it, I'm trying to use proxy as
$telegram = new Longman\TelegramBot\Telegram('API-key', 'Bot-name');
$data = [
'chat_id' => 12345678,
'text' => 'Test message',
];
Longman\TelegramBot\Request::setClient(new \GuzzleHttp\Client([
'base_uri' => 'https://api.telegram.org',
'proxy' => 'socks5://218.248.73.193:1080',
'verify' => false,
]));
$result = Longman\TelegramBot\Request::sendMessage($data);
but it return Telegram returned an invalid response! Please review your bot name and API key.
How to avoid this problem? Thanks in advance!

Longman\TelegramBot\Request::setClient(new \GuzzleHttp\Client([
'base_uri' => 'https://api.telegram.org',
'proxy' => 'socks5://218.248.73.193:1080',
'verify' => false,
'timeout' => 10, //*****add this
]));

Related

How to connect WordPress / WooCommerce to a REST API with PHP?

I am trying to write my first REST API with PHP. We want to send shipping data to the shipping company and get labels for printing in return. The shipping company has given me test api credentials.
So I started to write a plugin with this code inside:
$url = "https://mywebsite/endpoint/";
$username = "myusername";
$password = "mypassword";
$data = array(
'key1' => 'value1',
'key2' => 'value2'
);
$response = wp_remote_post( $url, array(
'body' => $data,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
)
);
var_dump($response); // not being called
When I run this code, I get a white screen with no error message, the last line with var_dump($response) is not being called. I am a bit stuck here because I se no success and no error...what could it be? Why this behaviour of the remote server? Does it mean "sorry, wrong credentials" or "data has the wrong format"? I have the feeling that the server doesn't even notice that I'm trying to contact him...
I tried a number of other variatons of the above code that I found somwehere online, and also outside of WordPress, but no success.
The shipping company gave me a documentation. It says:
HTTP Method: POST
Authentication Header: Basic Authentication (user name/ password)
so I thought I could do no wrong when trying that. Hm.
I found a solution, so I can share it here with you.
I installed the free Software Postman and created a simple POST query there. After entering $url, $username and $password I ran a query and it worked well. the good thing abort Postman is that it creates code snippets for you. "very_long_cryptic_string" is obviously created from $username and $password. The PHP for my case goes like this, and it works:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic very_long_cryptic_string'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

After Move http to https of wordpress thrid party API's not working

I have implemented the sigle signon in worpress with Java with Aws cognito.
it is working fine with http but after moving http to https. API's are not working and throwing the error below:
WP_Error Object
(
[errors] => Array
(
[http_request_failed] => Array
(
[0] => cURL error 35: error:1408F10B:SSL routines:ssl3_get_record:wrong version number
)
)
[error_data] => Array
(
)
)
I have consumed the get and Post API like below:
$bodyData = array(
'username' => $_POST['username'],
'password' => $_POST['password']
);
$response = wp_remote_post('https://example.com:8083/digitalIdentityProvider/login',
array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '2.0',
'sslverify' => true,
'blocking' => true,
'headers' => array('Content-Type'=> 'application/json'),
'body' => json_encode($bodyData) ,
'cookies' => array()
));
Could you please someone guide or suggest ?
Thanks.
After playing with wordpress code and AWS, I found the issue with Aws ELB and Load Balancer .
SSL Certificate has applied to LB and Third party API and wordpress were hosted on AWS EC2 and for EC2 there was not SSL certificate and I was trying to access API with
HTTPS:
https://example.com:8083/digitalIdentityProvider/login hence request was not verifying over there hence I was not receiving any response from API.
I have changed API call
from https://example.com:8083/digitalIdentityProvider/login
to http://example.com:8083/digitalIdentityProvider/login
after change it https to http. it working fine.

Handle incoming Array from Guzzle

So i have been trying to get guzzle working, i can send a post request which sends some kind of array and I would like to know, how can i receive it at an endpoint? i can handle things like if(isset($_POST['nameHere'])) but not the array thingy
$client = new Client();
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'email' => 'test#gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
],
'debug' => true
]);
echo '<pre>' . print_r((string)$response->getBody(), true) . '</pre>';
}
ps: My custom laravel application is the sender, my wordpress site is the receiver.
Got it working, i was missing the .php ending in my url, because i chose not to test with httpbin but directly with my api.
and in wordpress i dont get anything because this is the sendercode i published not the receiver (wordpress). i wouldnt know how to test it. thanks anyway

How to send data back to woocommerce custom gateway after processing

I am trying to get response data back to my payment gateway with no success.
This is the code snippet within my woocommerce extension that sends data to my payment processor(index.php)
$environment_url = "https://example.com/api/index.php";
$payload = array(
// Credentials and API Info
"x_tran_key" => $this->trans_key,
"x_login" => $this->api_login,
"x_version" => "3.1",
// Order total
"x_amount" => $customer_order->order_total
);
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
I get the data very fine to the url(index.php). The issue is getting the response back to my custom woocommerce extension so that I can complete payment_process()
Most resources I have found online only talk about sending data, but not how the response or result is sent back to the querying resource file.
Any help will be highly appreciated. Thank you.
Just did a simple print_r and it did the trick, I got my results in an array and used print_r
If anybody encounters the same problem as I did, I hope this has saved your time.

how to create uninstall webhook in shopify using laravel wrapper?

i am trying to create webhook using the api call inlaravel but it gives me the error "The requested URL returned error: 406 Not Acceptable".
my code is as bellow.
$webhookData = [
'webhook' => [
'topic' => 'app/uninstalled',
'address' => url('/uninstall'),
'fields' => 'name',
'format' => 'json'
]
];
$uninstall = $sh->call(['URL' => 'https://' . $shop . '/admin/webhooks.json', 'METHOD' => 'POST', 'DATA' => $webhookData]);
There are certain things you need to take care of while creating the Shopify webhooks.
The address field should be a globally accessible URL (ex: https://example.com/webhook/uninstall.php)
You can not give localhost links for creating webhooks.
I think the address field is not passed with a valid URL in your post request.

Resources