Closed amirmasoud closed 9 years ago
Can you post your code please?
same for sending audio and video.
class TelegramController extends Controller
{
protected $telegram;
private $lastOffset = 0;
public function __construct()
{
$this->telegram = new Telegram(getenv('TELEGRAM_BOT_TOKEN'));
}
public function updates()
{
foreach ($this->telegram->getUpdates() as $item) {
$keyboard = [
['poem', 'pic', 'video', 'song']
];
$reply_markup = $this->telegram->replyKeyboardMarkup($keyboard, true, true);
$chat_id = $item['message']['chat']['id'];
if($item['message']['text'] == 'شعر')
{
$this->telegram->sendChatAction($chat_id, 'typing');
$poet = json_decode(file_get_contents('http://c.ganjoor.net/beyt-json.php'));
$message = $poet->m1 . PHP_EOL . $poet->m2 . PHP_EOL . PHP_EOL . $poet->poet . PHP_EOL . $poet->url;
$response = $this->telegram->sendMessage($chat_id, $message, true, null, $reply_markup);
}
elseif($item['message']['text'] == 'pic')
{
$this->telegram->sendChatAction($chat_id, 'upload_photo');
$url = 'http://www.savethecat.com/wp-content/uploads/2015/06/cats.jpg';
$caption = 'sample caption';
$response = $this->telegram->sendPhoto($chat_id, $url, $caption);
}
elseif($item['message']['text'] == 'video')
{
$this->telegram->sendChatAction($chat_id, 'upload_video');
$url = 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4';
$response = $this->telegram->sendVideo($chat_id, $url);
}
elseif($item['message']['text'] == 'song')
{
$this->telegram->sendChatAction($chat_id, 'upload_audio');
$url = 'http://www.tonycuffe.com/mp3/cairnomount.mp3';
$response = $this->telegram->sendAudio($chat_id, $url);
}
else
{
$response = $this->telegram->sendMessage($chat_id, 'Invalid response.', true, null, $reply_markup);
}
return $this->telegram->getUpdates($this->lastMessageId());
}
and the error:
Whoops, looks like something went wrong.
1/1
InvalidArgumentException in functions.php line 113:
Invalid resource type: integer
in functions.php line 113
at GuzzleHttp\Psr7\stream_for('95607058') in MultipartStream.php line 87
at MultipartStream->addElement(object(AppendStream), array('name' => 'chat_id', 'contents' => '95607058')) in MultipartStream.php line 70
at MultipartStream->createStream(array(array('name' => 'chat_id', 'contents' => '95607058'), array('name' => 'audio', 'contents' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3'), array('name' => 'reply_to_message_id', 'contents' => null), array('name' => 'reply_markup', 'contents' => null))) in MultipartStream.php line 31
at MultipartStream->__construct(array(array('name' => 'chat_id', 'contents' => '95607058'), array('name' => 'audio', 'contents' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3'), array('name' => 'reply_to_message_id', 'contents' => null), array('name' => 'reply_markup', 'contents' => null))) in Client.php line 300
at Client->applyOptions(object(Request), array('timeout' => '60', 'connect_timeout' => '10', 'synchronous' => true, 'handler' => object(HandlerStack), 'allow_redirects' => array('max' => '5', 'protocols' => array('http', 'https'), 'strict' => false, 'referer' => false), 'http_errors' => true, 'decode_content' => true, 'verify' => true, 'cookies' => false, '_conditional' => array('User-Agent' => 'GuzzleHttp/6.0.2 curl/7.39.0 PHP/5.6.3'))) in Client.php line 262
at Client->transfer(object(Request), array('timeout' => '60', 'connect_timeout' => '10', 'synchronous' => true, 'multipart' => array(array('name' => 'chat_id', 'contents' => '95607058'), array('name' => 'audio', 'contents' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3'), array('name' => 'reply_to_message_id', 'contents' => null), array('name' => 'reply_markup', 'contents' => null)), 'handler' => object(HandlerStack), 'allow_redirects' => array('max' => '5', 'protocols' => array('http', 'https'), 'strict' => false, 'referer' => false), 'http_errors' => true, 'decode_content' => true, 'verify' => true, 'cookies' => false, '_conditional' => array('User-Agent' => 'GuzzleHttp/6.0.2 curl/7.39.0 PHP/5.6.3'))) in Client.php line 124
at Client->requestAsync('POST', 'https://api.telegram.org/bot102643193:AAERyAeukNGz1KPGqLSTqh1ewkNLbP7Ajqc/sendAudio', array('headers' => array('User-Agent' => 'Telegram Bot PHP SDK v0.2.2 - (https://github.com/irazasyed/telegram-bot-sdk)'), 'body' => null, 'timeout' => '60', 'connect_timeout' => '10', 'synchronous' => true, 'multipart' => array(array('name' => 'chat_id', 'contents' => '95607058'), array('name' => 'audio', 'contents' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3'), array('name' => 'reply_to_message_id', 'contents' => null), array('name' => 'reply_markup', 'contents' => null)))) in GuzzleHttpClient.php line 85
at GuzzleHttpClient->send('https://api.telegram.org/bot102643193:AAERyAeukNGz1KPGqLSTqh1ewkNLbP7Ajqc/sendAudio', 'POST', array('User-Agent' => 'Telegram Bot PHP SDK v0.2.2 - (https://github.com/irazasyed/telegram-bot-sdk)'), array('multipart' => array(array('name' => 'chat_id', 'contents' => '95607058'), array('name' => 'audio', 'contents' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3'), array('name' => 'reply_to_message_id', 'contents' => null), array('name' => 'reply_markup', 'contents' => null))), '60', false) in TelegramClient.php line 116
at TelegramClient->sendRequest(object(TelegramRequest)) in Telegram.php line 603
at Telegram->sendRequest('POST', 'sendAudio', array('multipart' => array(array('name' => 'chat_id', 'contents' => '95607058'), array('name' => 'audio', 'contents' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3'), array('name' => 'reply_to_message_id', 'contents' => null), array('name' => 'reply_markup', 'contents' => null)))) in Telegram.php line 550
at Telegram->post('sendAudio', array('multipart' => array(array('name' => 'chat_id', 'contents' => '95607058'), array('name' => 'audio', 'contents' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3'), array('name' => 'reply_to_message_id', 'contents' => null), array('name' => 'reply_markup', 'contents' => null)))) in Telegram.php line 580
at Telegram->uploadFile('sendAudio', array('chat_id' => '95607058', 'audio' => 'http://www.tonycuffe.com/mp3/cairnomount.mp3', 'reply_to_message_id' => null, 'reply_markup' => null)) in Telegram.php line 257
at Telegram->sendAudio('95607058', 'http://www.tonycuffe.com/mp3/cairnomount.mp3') in TelegramController.php line 105
at TelegramController->updates()
at call_user_func_array(array(object(TelegramController), 'updates'), array()) in Controller.php line 256
at Controller->callAction('updates', array()) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(TelegramController), object(Route), 'updates') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(TelegramController), object(Route), object(Request), 'updates') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\TelegramController', 'updates') in Route.php line 201
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 704
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 229
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 118
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 86
at Kernel->handle(object(Request)) in index.php line 54
Thanks for posting. I'm testing now. I'll let you know soon.
Thanks.
I just pushed an update - v0.2.3 which fixes that integer issue. However, I see that you're trying to upload remote files which is invalid. Telegram doesn't support that. You're supposed to give local path to your file.
Also, It seems like you're using Laravel (Assuming based on the controller). Here's how i would do that instead:
<?php
namespace app\Http\Controllers;
class TelegramController extends Controller
{
protected $telegram;
private $lastOffset = 0;
public function __construct()
{
$this->telegram = app('telegram');
}
public function updates()
{
foreach ($this->telegram->getUpdates() as $item) {
$keyboard = [
['poem', 'pic', 'video', 'song']
];
$reply_markup = $this->telegram->replyKeyboardMarkup($keyboard, true, true);
$chat_id = $item['message']['chat']['id'];
$message = $item['message']->getText();
switch ($message) {
case 'شعر':
$this->telegram->sendChatAction($chat_id, 'typing');
$poet = json_decode(file_get_contents('http://c.ganjoor.net/beyt-json.php'));
$message = $poet->m1 . PHP_EOL . $poet->m2 . PHP_EOL . PHP_EOL . $poet->poet . PHP_EOL . $poet->url;
$response = $this->telegram->sendMessage($chat_id, $message, true, null, $reply_markup);
break;
case 'pic':
$this->telegram->sendChatAction($chat_id, 'upload_photo');
$file = storage_path('cats.jpg');
$caption = 'sample caption';
$response = $this->telegram->sendPhoto($chat_id, $file, $caption);
break;
case 'video':
$this->telegram->sendChatAction($chat_id, 'upload_video');
$file = storage_path('big_buck_bunny.mp4');
$response = $this->telegram->sendVideo($chat_id, $file);
break;
case 'song':
$this->telegram->sendChatAction($chat_id, 'upload_audio');
$file = storage_path('cairnomount.mp3');
$response = $this->telegram->sendAudio($chat_id, $file);
break;
default:
$response = $this->telegram->sendMessage($chat_id, 'Invalid response.', true, null, $reply_markup);
break;
}
return $this->telegram->getUpdates($this->lastMessageId());
}
}
}
This would work if you add service provider to your config and then either have the token in env or in config file (After you publish ofc).
Edit: I'm using storage_path
which comes with Laravel. The files are stored under path/to/laravel/storage/<file name>
Let me know if it works for you. I'll keep this open until tomorrow.
yes I set the token in the .env file and also added service provider in the config. It's working great, thanks :+1:
I face Invalid resource type: integer when trying to send a photo. my sendPhoto method is ok, what should I do?