Skip to content

Instantly share code, notes, and snippets.

@mpociot
Created January 2, 2017 23:29
Show Gist options
  • Save mpociot/926dbecec28d00fc21a31b0b378ac20e to your computer and use it in GitHub Desktop.
Save mpociot/926dbecec28d00fc21a31b0b378ac20e to your computer and use it in GitHub Desktop.
BotMan messaging driver that adds support for Telegram inline queries.
<?php
namespace Mpociot\BotMan\Drivers;
use Mpociot\BotMan\Answer;
use Mpociot\BotMan\Message;
use Mpociot\BotMan\Question;
use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;
use Mpociot\BotMan\Messages\Message as IncomingMessage;
class TelegramInlineQueryDriver extends Driver
{
/** @var Collection|ParameterBag */
protected $payload;
/** @var Collection */
protected $event;
const DRIVER_NAME = 'TelegramInlineQuery';
/**
* @param Request $request
*/
public function buildPayload(Request $request)
{
$this->payload = new ParameterBag((array) json_decode($request->getContent(), true));
$this->event = Collection::make($this->payload->get('inline_query'));
}
/**
* Return the driver name.
*
* @return string
*/
public function getName()
{
return self::DRIVER_NAME;
}
/**
* Determine if the request is for this driver.
*
* @return bool
*/
public function matchesRequest()
{
return ! is_null($this->payload->get('inline_query')) && ! is_null($this->payload->get('update_id'));
}
/**
* @param Message $message
* @return Answer
*/
public function getConversationAnswer(Message $message)
{
return Answer::create($message->getMessage());
}
/**
* Retrieve the chat message.
*
* @return array
*/
public function getMessages()
{
return [new Message($this->event->get('query'), $this->event->get('from')['id'], $this->event->get('id'), $this->event)];
}
/**
* @return bool
*/
public function isBot()
{
return false;
}
/**
* @param array|string|Question|IncomingMessage $messages
* @param Message $matchingMessage
* @param array $additionalParameters
* @return Response|false
*/
public function reply($messages, $matchingMessage, $additionalParameters = [])
{
$parameters = array_merge([
'cache_time' => 0,
'inline_query_id' => $matchingMessage->getChannel(),
], $additionalParameters);
$results = [];
if (!is_array($messages)) {
$messages = [$messages];
}
foreach ($messages as $message) {
$result = [
'type' => 'article',
'id' => uniqid()
];
/*
* Questions are not possible in combination with
* Telegram inline queries.
*/
if ($message instanceof Question) {
return false;
} elseif ($message instanceof IncomingMessage) {
$result['title'] = $message->getMessage();
$result['input_message_content'] = [
'message_text' => $message->getMessage()
];
if (! is_null($message->getImage())) {
$result['thumb_url'] = $message->getImage();
}
} elseif (is_array($message)) {
$result = $message;
}
$results[] = $result;
}
$parameters['results'] = json_encode($results);
return $this->http->post('https://api.telegram.org/bot'.$this->config->get('telegram_token').'/answerInlineQuery', [], $parameters);
}
/**
* @return bool
*/
public function isConfigured()
{
return ! is_null($this->config->get('telegram_token'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment