sendMediaGroup telegram PHP - 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)

Related

Telegram sendPhoto with multipart/form-data not working?

Hi I am trying to send an image. The documentation states that I can send a file using multipart/form-data.
Here is my code:
// I checked it, there really is a file.
$file = File::get(Storage::disk('local')->path('test.jpeg')) // it's the same as file_get_contents();
// Here I use the longman/telegram-bot library.
$serverResponse = Request::sendPhoto([
'chat_id' => $this->tg_user_chat_id,
'photo' => $file
]);
// Here I use Guzzle because I thought that there might be an
// error due to the longman/telegram-bot library.
$client = new Client();
$response = $client->post("https://api.telegram.org/$telegram->botToken/sendPhoto", [
'multipart' => [
[
'name' => 'photo',
'contents' => $file
],
[
'name' => 'chat_id',
'contents' => $this->tg_user_chat_id
]
]
]);
Log::info('_response', ['_' => $response->getBody()]);
Log::info(env('APP_URL') . "/storage/$url");
Log::info('response:', ['_' => $serverResponse->getResult()]);
Log::info('ok:', ['_' => $serverResponse->getOk()]);
Log::info('error_code:', ['_' => $serverResponse->getErrorCode()]);
Log::info('raw_data:', ['_' => $serverResponse->getRawData()]);
In both cases, I get this response:
{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: invalid file HTTP URL specified: Wrong URL host\"}
Other download methods (by ID and by link) work. Can anyone please point me in the right direction?
Using the php-telegram-bot
library, sendPhoto can be used like so:
<?php
require __DIR__ . '/vendor/autoload.php';
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Request;
// File
$file = Request::encodeFile('/tmp/image.jpeg');
// Bot
$key = '859163076:something';
$telegram = new Telegram($key);
// sendPhoto
$chatId = 000001;
$serverResponse = Request::sendPhoto([
'chat_id' => $chatId,
'photo' => $file
]);
The trick is to use Request::encodeFile to read the local image.

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
}
}

make the variable as string in laravel

how are you ?
I'm using Guzzle to send post method but when i tried to send the post method with variable getting error so i tried to convert it to sting but still same
this is my current code
if $ mobile value is 55454545445 , them the 'numbers' => "{$mobile}" will be different not "55454545445"
$user = Auth::user();
$mobile = $user->mobile;
$client = new Client();
$booking = Booking::where('id', '=', e($id))->first();
if($booking)
{
$booking->booking_status_id = 3;
$booking->save();
$client = new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$data = array('userName' => "test",
'apiKey' => "1",
'numbers' => "{$mobile}",
'userSender' => "sender",
'msg' =>'msg',
'msgEncoding' => "UTF8",);
$dataJson = json_encode($data);
$response = $client->post('https://www.test.test',
['body' => $dataJson]
);
i used this
"0{$mobile}"
instead of
"{$mobile}"
and its work

Request to RingCentral API

I am trying to send a request to RingCentral API. The link to documentation is https://developers.ringcentral.com/api-reference/Fax/createFaxMessage If I don't specify faxResolution or coverIndex everything goes well, fax could be sent. But if I add faxResolution param like in the code below, I receive error "Parameter [faxResolution] value is invalid", "errorCode" : "CMN-101". The same thing with coverIndex param. My client is GuzzleHttp 6.3
$token = $this->ringcentral->platform()->auth()->data()['access_token'];
$a = array();
foreach ($destination_numbers as $number) {
$a[] = [
'name' => 'to',
'contents' => $number,
'headers' => ['Content-Type' => 'multipart/form-data']
];
}
$a[] = [
'name' => 'faxResolution',
'contents' => 'High',
'headers' => ['Content-Type' => 'multipart/form-data']
];
foreach ($attachments as $attachment) {
$file_pointer = fopen($attachment, 'r');
$mime = mime_content_type($attachment);
$a[] = [
'name' => 'attachment',
'contents' => $file_pointer,
'headers' => ['Content-Type' => $mime]
];
}
$client = new Client();
try {
$response = $client->request('POST', url(config('services.ringcentral.app_url')) . '/restapi/v1.0/account/~/extension/~/fax', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token
],
'multipart' => $a
]);
$response = json_decode($response->getBody(), true);
} catch (\GuzzleHttp\Exception\ClientException $e) {
echo($e->getResponse()->getBody()->getContents());
}
Here's what RingCentral suggests when using PHP. I included all of what they suggested, but just look at the part about faxResolution (2/3 of the way down)
<?php
// https://developers.ringcentral.com/my-account.html#/applications
// Find your credentials at the above url, set them as environment variables, or enter them below
// PATH PARAMETERS
$accountId = '<ENTER VALUE>';
$extensionId = '<ENTER VALUE>';
$recipient = '<ENTER VALUE>';
require('vendor/autoload.php');
$rcsdk = new RingCentral\SDK\SDK(getenv('clientId'), getenv('clientSecret'), getenv('serverURL'));
$platform = $rcsdk->platform();
$platform->login(getenv('username'), getenv('extension'), getenv('password'));
$request = $rcsdk->createMultipartBuilder()
->setBody(array(
'to' => array(array('phoneNumber' => $recipient)),
'faxResolution' => 'High',
))
->add(fopen('fax.jpg', 'r'))
->request("/restapi/v1.0/account/{$accountId}/extension/{$extensionId}/fax");
$r = $platform->sendRequest($request);
?>

perform an asynchronous query in PHP using "Guzzle"

make an asynchronous query with PHP using Guzzle and not wait for the result.
how to retrieve JSON content into another PHP file?
<?php
require '/tools/guzzle-master/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$url = 'https://xxxxxxx/receive.php;
/*creating a client*/
$client = new Client();
/*JSON formatting */
$param = array(
'order_id' => 4544,
'type' => 0,
'amount' => 1266,
'fullname' => 'qdxeq',
'account' => 'dqad',
'callback_url' => 'http://yuexingy.top/Withdraw/WithdrawCallback.aspx',
'device_type' => 'dqd',
'device_id' => 'dafwe',
'device_ip' => 'dwe',
);
$json = json_encode($param);//json encoding
$data = array('json'=>$json);
$req = new Request('POST',$url, $data);
//sending the asynchronous request
$promise = $client->sendAsync($req,['timeout' => 10])->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
?>

Categories