Code Monkey home page Code Monkey logo

Comments (4)

mityukov avatar mityukov commented on May 21, 2024 1

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.

irazasyed avatar irazasyed commented on May 21, 2024

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.

irazasyed avatar irazasyed commented on May 21, 2024

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.

irazasyed avatar irazasyed commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.