Skip to content

How to send a message to channel without user interaction? #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
frenzis opened this issue Jan 5, 2016 · 12 comments
Closed

How to send a message to channel without user interaction? #71

frenzis opened this issue Jan 5, 2016 · 12 comments

Comments

@frenzis
Copy link

frenzis commented Jan 5, 2016

Hi, and thank you for your work. I'm trying to send a message to a channel without user interaction. My Bot is admin of the channel. Did you have a simple working example using the sendMessage method?

@MBoretto
Copy link
Collaborator

MBoretto commented Jan 5, 2016

Hello did you seen the command send to channel in the admin dir? This provide what you're looking for, to use this command you should enabled the admin interface of the bot (not the channel) as explained in the readme. Let me know if the documentation is enough clear ask if you have further questions

@frenzis
Copy link
Author

frenzis commented Jan 5, 2016

Thank you Marco. The example is clear, but I would like to send a message to a channel without interacting with the bot in the Telegram app (ie: without sending a /sendtochannel command). For example: I would like to read a rss feed and send a message to the channel (via the bot) every time a new item is published. I know I can use a simple script (like this one), but I like your classe and I would like to use it without any other code.

@MBoretto
Copy link
Collaborator

MBoretto commented Jan 5, 2016

       use Longman\TelegramBot\Request;
        $data = [];
        $data['chat_id'] = '@your_channel';
        $data['text'] = 'Hello';
        $result = Request::sendMessage($data);
        if ($result->isOk()) {
            echo 'Message sent succesfully to: '.$data['chat_id'] ;
        } else {
            echo 'Sorry message not sent to: '.$data['chat_id'] ;
        }

Thats all you need to use the class without the bot interface, notice that you should set a cron to read the RSS.

@frenzis
Copy link
Author

frenzis commented Jan 5, 2016

Perfect! Thank you.

@frenzis frenzis closed this as completed Jan 5, 2016
@ariefbayu
Copy link

here's my code for broadcast message to particular chat: https://gist.github.com/ariefbayu/e3ffe27f9c50cb674edd

@noplanman
Copy link
Member

Thanks for sharing your CLI-friendly version 😃

A few notes about your code if I may:

  • Telegram constructor already handles the Request::initialize() method, so you don't need to do that separately.
  • Also, you probably want to use && instead of || here:

if ($message !== '' && $telegramId !== '') {...}

@ariefbayu
Copy link

Awesome, thank you for the suggestion!

@MBoretto
Copy link
Collaborator

@ariefbayu great! This can help other users! Feel free to open a wiki page with your example that point to your code!

@ariefbayu
Copy link

@noplanman
Copy link
Member

Thanks @ariefbayu!

I've forked your gist and made the code PSR2 friendly, then updated the wiki page.
Feel free to update your gist and add a link to it on the wiki page if you like 😃

Also, it would make sense to add at least a bit of error handling, like some output if no chat id or message is passed. Just a simple Usage: ... message so the user can see it.
And if the API key and bot name haven't been changed, the request shouldn't even be sent to Telegram, as it will definitely fail.

And I was playing with the idea of setting the $API_KEY and $BOT_NAME variables from environment variables (getenv), that way the script wouldn't even need to be modified by hand.
What do you think?

@ebrahimporiyan
Copy link

@noplanman @frenzis @MBoretto @ariefbayu @boukeversteegh

Hi. Is there a script that I can add more members to my grammar channel.
For example, members are forced to enter the channel .
For example, some telegraph channels do this.
Please, if anyone knows, help me
my gmail ([email protected])
my telegram number 00989366043698

@noplanman
Copy link
Member

@ebrahimporiyan No, that is not supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants