Comments (4)
Here's my workaround for the moment:
// App\Notifications\MyFancyNotification.php
public function via($notifiable)
{
return [ \App\Channels\MultiTelegramChannel::class ];
}
public function toMultiTelegram($notifiable)
{
return [
TelegramLocation::create()
->options(['text' => $this->getTelegramText()])
->latitude($this->activity->getCoords()->lat)
->longitude($this->activity->getCoords()->lon),
TelegramMessage::create()
->content($this->getTelegramText()),
];
}
// app/Channels/MultiTelegramChannel.php
<?php
namespace App\Channels;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\Exceptions\CouldNotSendNotification;
use NotificationChannels\Telegram\Telegram;
use NotificationChannels\Telegram\TelegramFile;
use NotificationChannels\Telegram\TelegramLocation;
use NotificationChannels\Telegram\TelegramMessage;
class MultiTelegramChannel extends \NotificationChannels\Telegram\TelegramChannel
{
/**
* Channel constructor.
*
* @param Telegram $telegram
*/
public function __construct()
{
// $this->telegram = resolve(Telegram::class); // doesn't workβ¦
$this->telegram = new Telegram(
config('services.telegram-bot-api.token'),
app(HttpClient::class),
config('services.telegram-bot-api.base_uri')
);
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param Notification $notification
*
* @throws CouldNotSendNotification
* @return null|array
*/
public function send($notifiable, Notification $notification): ?array
{
$messages = $notification->toMultiTelegram($notifiable);
$res = [];
foreach ($messages as $message) {
if (is_string($message)) {
$message = TelegramMessage::create($message);
}
if ($message->toNotGiven()) {
if (! $to = $notifiable->routeNotificationFor('telegram', $notification)) {
return null;
}
$message->to($to);
}
if ($message->hasToken()) {
$this->telegram->setToken($message->token);
}
$params = $message->toArray();
if ($message instanceof TelegramMessage) {
$response = $this->telegram->sendMessage($params);
} elseif ($message instanceof TelegramLocation) {
$response = $this->telegram->sendLocation($params);
} elseif ($message instanceof TelegramFile) {
$response = $this->telegram->sendFile($params, $message->type, $message->hasFile());
} else {
return null;
}
$res[] = json_decode($response->getBody()->getContents(), true);
}
return $res;
}
}
β¦ though, I love the "slightly hackish way" as well π
And the final though: it seems you can attach some Title and Address data to the Venue type of message. Maybe, worth adding? https://core.telegram.org/bots/api#sendvenue
from telegram.
Hi, Telegram Bot API doesn't support text/caption/message field for the location method. You can refer the sendlocation official docs to get an idea of supported params.
We also can't merge the way you've suggested. However, you can create two different notification classes and trigger them at once to achieve what you're looking to do.
Hope that helps!
from telegram.
You can also try this slightly hackish way:
use NotificationChannels\Telegram\Telegram;
use NotificationChannels\Telegram\TelegramMessage;
...
public function toTelegram($notifiable)
{
$text = 'Here is the location!';
$to = 'USER CHAT_ID';
$message = TelegramMessage::create()
->to($to)
->content($text)
->toArray();
$telegram = new Telegram(config('services.telegram-bot-api.token'));
$telegram->sendMessage($message);
return TelegramLocation::create()
->latitude($this->activity->getCoords()->lat)
->longitude($this->activity->getCoords()->lon);
}
I haven't tested this, so please try and see if it works! This is based of your code ofc.
from telegram.
That's a decent workaround π
Venue attachment isn't supported right now but feel free to add and send a PR if you need it :)
from telegram.
Related Issues (20)
- Laravel 10 compatibility HOT 5
- How to add message_thread_id to TelegramMessage::create() HOT 1
- update message(Custom endpoint overwriting needed) HOT 1
- The communication with Telegram failed HOT 3
- Posting in topics HOT 2
- sending album photos HOT 1
- How to send to multiple Telegram bots HOT 1
- Error: Call to a member function toNotGiven() on null HOT 2
- Cannot get message_id of sent message HOT 9
- Is there a way to send multiple images in one message? HOT 2
- it.tdlight.client.TelegramError: 400: Chat not found HOT 3
- Latest Commit isn't included in a tagged version HOT 1
- Telegram responded with an error `403 - Forbidden: bots can't send messages to bots HOT 1
- Please support laravel 11 HOT 1
- Unable to retrieve chat id or full request from Exception
- Expand TelegramMessage class HOT 1
- Telegram responded with an error `403 - Forbidden: bot was blocked by the user`
- How do I catch a 400 error exception?
- Call to a member function toNotGiven() on null
- Enhancement Request: Add Multi-file/Photo Sending Feature Using Telegram API's sendMediaGroup Method HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from telegram.