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

Send message automatically, without user input, at specified time. #107

Closed
reach2jeyan opened this Issue Feb 24, 2017 · 13 comments

Comments

Projects
None yet
5 participants
@reach2jeyan

reach2jeyan commented Feb 24, 2017

I am sorry, if the question is basic, but as far as I saw the methods of this gem, sends replies, when it receives an input from the user.

Does this gem support the feature like, sending message at a specific time or every hour or when the time lapses?

@ivanovaleksey

This comment has been minimized.

Show comment
Hide comment
@ivanovaleksey

ivanovaleksey Feb 24, 2017

Contributor

As I can see this is one of the most frequently asked question ☺️

Of course you can send a message to a user not as a reply but as notification (seems to be your case).
But first you need to obtain chat_id. And of course user must press Start, i.e. the bot shouldn't be blocked by the user.

Given readme example. On receiving /start you just need to store message.chat.id somewhere. Then you can send messages to that user whenever you want.

Note that different Telegram client use different Start command message. The most common is /start but MacOS client (as I can remember) send /start start. So it would be better to use some regexp logic to route this command properly.

Contributor

ivanovaleksey commented Feb 24, 2017

As I can see this is one of the most frequently asked question ☺️

Of course you can send a message to a user not as a reply but as notification (seems to be your case).
But first you need to obtain chat_id. And of course user must press Start, i.e. the bot shouldn't be blocked by the user.

Given readme example. On receiving /start you just need to store message.chat.id somewhere. Then you can send messages to that user whenever you want.

Note that different Telegram client use different Start command message. The most common is /start but MacOS client (as I can remember) send /start start. So it would be better to use some regexp logic to route this command properly.

@reach2jeyan

This comment has been minimized.

Show comment
Hide comment
@reach2jeyan

reach2jeyan Feb 25, 2017

Thanks for your response. I however, Slightly confused with this line

On receiving /start you just need to store message.chat.id somewhere.

Do you mean to say /Start is a one time message sent by the user(which is me) to fetch the chat id? Or does this bot need to everytime receive /Start from the user(which is me) to send a message? Can you clarify this?

I am having a ruby program a scheduler that runs jobs at a specific time. I wanted that scheduler to also, have this as a seperate job, that for example every day at 4 o clock, send a message in bot. The example that you have shared works well, When I send an input to the bot and bot replies me back . How without any input from the user, everyday, I can get a notification? Can I use just the token and username?

reach2jeyan commented Feb 25, 2017

Thanks for your response. I however, Slightly confused with this line

On receiving /start you just need to store message.chat.id somewhere.

Do you mean to say /Start is a one time message sent by the user(which is me) to fetch the chat id? Or does this bot need to everytime receive /Start from the user(which is me) to send a message? Can you clarify this?

I am having a ruby program a scheduler that runs jobs at a specific time. I wanted that scheduler to also, have this as a seperate job, that for example every day at 4 o clock, send a message in bot. The example that you have shared works well, When I send an input to the bot and bot replies me back . How without any input from the user, everyday, I can get a notification? Can I use just the token and username?

@ivanovaleksey

This comment has been minimized.

Show comment
Hide comment
@ivanovaleksey

ivanovaleksey Feb 25, 2017

Contributor

No, you can't send messages using username. You should use chat_id.

/start text is sent when user press Start within chat with your bot. So when you receive this text, you should store message.chat.id. This would be chat_id which you can use to send messages to the user anytime you want.

Contributor

ivanovaleksey commented Feb 25, 2017

No, you can't send messages using username. You should use chat_id.

/start text is sent when user press Start within chat with your bot. So when you receive this text, you should store message.chat.id. This would be chat_id which you can use to send messages to the user anytime you want.

@reach2jeyan

This comment has been minimized.

Show comment
Hide comment
@reach2jeyan

reach2jeyan Feb 25, 2017

Okay! Let me try this and get back to you in a while.

reach2jeyan commented Feb 25, 2017

Okay! Let me try this and get back to you in a while.

@reach2jeyan

This comment has been minimized.

Show comment
Hide comment
@reach2jeyan

reach2jeyan Feb 25, 2017

Thanks a lot! I was able to implement it successfully with what you said.

So, In order to implement this in group? like send me this message in the group? Do I have to fetch any additional information like group_id?

reach2jeyan commented Feb 25, 2017

Thanks a lot! I was able to implement it successfully with what you said.

So, In order to implement this in group? like send me this message in the group? Do I have to fetch any additional information like group_id?

@ivanovaleksey

This comment has been minimized.

Show comment
Hide comment
@ivanovaleksey

ivanovaleksey Feb 25, 2017

Contributor

In group chat you would have message.from which stands for the user who send the message and message.chat which stands for the whole group chat. So you can grab message.chat.id to send messages to the whole group (for me it is negative number but it is okay).

Telegram::Bot::Client.run(TOKEN) do |bot|
  bot.listen do |message|
    p message.from 
    #=> #<Telegram::Bot::Types::User:0x007fb20de27a68 @id=151031480, @first_name="Aleksey", @username="ialeksey", @last_name=nil>
    p message.chat 
    #=> #<Telegram::Bot::Types::Chat:0x007fb20de25bc8 @id=-101746809, @title="Chat", @type="group", @username=nil, @first_name=nil, @last_name=nil, @all_members_are_admins=nil>

    case message.text
    when '/start'
    when '/stop'
    when ...
    else
  end
end 
Contributor

ivanovaleksey commented Feb 25, 2017

In group chat you would have message.from which stands for the user who send the message and message.chat which stands for the whole group chat. So you can grab message.chat.id to send messages to the whole group (for me it is negative number but it is okay).

Telegram::Bot::Client.run(TOKEN) do |bot|
  bot.listen do |message|
    p message.from 
    #=> #<Telegram::Bot::Types::User:0x007fb20de27a68 @id=151031480, @first_name="Aleksey", @username="ialeksey", @last_name=nil>
    p message.chat 
    #=> #<Telegram::Bot::Types::Chat:0x007fb20de25bc8 @id=-101746809, @title="Chat", @type="group", @username=nil, @first_name=nil, @last_name=nil, @all_members_are_admins=nil>

    case message.text
    when '/start'
    when '/stop'
    when ...
    else
  end
end 
@reach2jeyan

This comment has been minimized.

Show comment
Hide comment
@reach2jeyan

reach2jeyan Feb 25, 2017

Thanks a lot for the very detailed information. It really helped me implementing this 💯

reach2jeyan commented Feb 25, 2017

Thanks a lot for the very detailed information. It really helped me implementing this 💯

@ivanovaleksey

This comment has been minimized.

Show comment
Hide comment
@ivanovaleksey

ivanovaleksey Feb 25, 2017

Contributor

Don't forget to close the issue ☺️

Contributor

ivanovaleksey commented Feb 25, 2017

Don't forget to close the issue ☺️

@neboduus

This comment has been minimized.

Show comment
Hide comment
@neboduus

neboduus Jul 27, 2017

Hi everybody! I apologize in advance for my bad English and for reopening this topic. I'm working on a bot (for learning purposes) which should help people to schedule activities and this amazing gem helped me a lot, if I had to implement TelegramAPI's by myself I think they would have passed months before deploying something reasonable. So first of all thanks for having developed it nd maintaining, hope in the future could.
Hence the bot offers 2 services. The first is conversational oriented to collect data and the second deals with pushing notification.
I am currently working on the pushing notification part and before diving myself into the implementation I red this discussion. My dilemma could seem trivial and I apologize but I am a beginner in the bot's world.
From what I understood I should be able to send a message to a chat having only the chat id. So if I stored it during the /start command interaction I could have everywhere else a piece of code similar to this:

Telegram::Bot::Client.run(TOKEN) do |bot|
   bot.send_message(chat_id: id, text: "hello world")

  # or like this
  # SomeLongPoolingHelper.poll do
  #    bot.send_message(chat_id: id, text: "hello world")
  # end

end 

Should this work? Or this block must necessary be placed into the webhook controller? I looken into lib/telegram folder but I did not find the answer.

Thanks in advance to anyone who will even only read this.

neboduus commented Jul 27, 2017

Hi everybody! I apologize in advance for my bad English and for reopening this topic. I'm working on a bot (for learning purposes) which should help people to schedule activities and this amazing gem helped me a lot, if I had to implement TelegramAPI's by myself I think they would have passed months before deploying something reasonable. So first of all thanks for having developed it nd maintaining, hope in the future could.
Hence the bot offers 2 services. The first is conversational oriented to collect data and the second deals with pushing notification.
I am currently working on the pushing notification part and before diving myself into the implementation I red this discussion. My dilemma could seem trivial and I apologize but I am a beginner in the bot's world.
From what I understood I should be able to send a message to a chat having only the chat id. So if I stored it during the /start command interaction I could have everywhere else a piece of code similar to this:

Telegram::Bot::Client.run(TOKEN) do |bot|
   bot.send_message(chat_id: id, text: "hello world")

  # or like this
  # SomeLongPoolingHelper.poll do
  #    bot.send_message(chat_id: id, text: "hello world")
  # end

end 

Should this work? Or this block must necessary be placed into the webhook controller? I looken into lib/telegram folder but I did not find the answer.

Thanks in advance to anyone who will even only read this.

@ivanovaleksey

This comment has been minimized.

Show comment
Hide comment
@ivanovaleksey

ivanovaleksey Jul 28, 2017

Contributor

@neboduus there are two different strategies how you can work with Bot API: long polling mode and webhook mode.
In long polling mode your process itself goes to API to receive new messages (bot.listen is used for it).
In webhook mode your process would receive requests from API with new messages.

In your case if you need just notify users, for example, every-hour-notification via cron (and not real-time conversation) you can try the following:

# initialize bot client and store it in constant
# that can be done in any file which is loaded at your application's boot process
BOT = Telegram::Bot::Client.new(TOKEN)

# anywhere in another file
BOT.send_message(chat_id: id, text: "hello world")

Your snippet is fine too, it would work.

Contributor

ivanovaleksey commented Jul 28, 2017

@neboduus there are two different strategies how you can work with Bot API: long polling mode and webhook mode.
In long polling mode your process itself goes to API to receive new messages (bot.listen is used for it).
In webhook mode your process would receive requests from API with new messages.

In your case if you need just notify users, for example, every-hour-notification via cron (and not real-time conversation) you can try the following:

# initialize bot client and store it in constant
# that can be done in any file which is loaded at your application's boot process
BOT = Telegram::Bot::Client.new(TOKEN)

# anywhere in another file
BOT.send_message(chat_id: id, text: "hello world")

Your snippet is fine too, it would work.

@neboduus

This comment has been minimized.

Show comment
Hide comment
@neboduus

neboduus Jul 28, 2017

Thanks @ivanovaleksey for your really fast response. This reconfirms me how much amazing are telegram's APIs and how easy to use it through this gem.

In order to respond to the user messages I use the gem in webhook mode with a Rails server and in order to send the notifications I defined a rake task.

I would like to share this minimalistic solution for pushing notification with everyone, maybe it could be useful or maybe not 😅

# rails_project/lib/tasks/notification_task.rake

require 'telegram/bot'

task :notify => :environment do
  token = Rails.application.secrets.bot_token
  user = User.where(some_parameters)

  Telegram::Bot::Client.run(token) do |bot|
    bot.api.send_message(chat_id: user.chat_id, text: 'Hello, world')
  end
end

And for launching the task we go to terminal and

rake notification_task

In this case I only send a notification to a User, but specifying

:notify => :environment

We include in the task all the environment of the Rails server so we can access all kinds of data present on the server and this allows us to enclose more complex logic into the task.

Thanks again @ivanovaleksey. Your response was really helpful!

neboduus commented Jul 28, 2017

Thanks @ivanovaleksey for your really fast response. This reconfirms me how much amazing are telegram's APIs and how easy to use it through this gem.

In order to respond to the user messages I use the gem in webhook mode with a Rails server and in order to send the notifications I defined a rake task.

I would like to share this minimalistic solution for pushing notification with everyone, maybe it could be useful or maybe not 😅

# rails_project/lib/tasks/notification_task.rake

require 'telegram/bot'

task :notify => :environment do
  token = Rails.application.secrets.bot_token
  user = User.where(some_parameters)

  Telegram::Bot::Client.run(token) do |bot|
    bot.api.send_message(chat_id: user.chat_id, text: 'Hello, world')
  end
end

And for launching the task we go to terminal and

rake notification_task

In this case I only send a notification to a User, but specifying

:notify => :environment

We include in the task all the environment of the Rails server so we can access all kinds of data present on the server and this allows us to enclose more complex logic into the task.

Thanks again @ivanovaleksey. Your response was really helpful!

@virgildantas

This comment has been minimized.

Show comment
Hide comment
@virgildantas

virgildantas Oct 29, 2017

use:
TOKEN = 'your bot token'
id = 'your chat id group or individual'
bot = Telegram::Bot::Client.new(TOKEN)
bot.api.send_message(chat_id: id, text: "hello world")

to send a message without use Telegram::Bot::Client.run(token) do |bot|
bot.api.send_message(chat_id: user.chat_id, text: 'Hello, world')
end

virgildantas commented Oct 29, 2017

use:
TOKEN = 'your bot token'
id = 'your chat id group or individual'
bot = Telegram::Bot::Client.new(TOKEN)
bot.api.send_message(chat_id: id, text: "hello world")

to send a message without use Telegram::Bot::Client.run(token) do |bot|
bot.api.send_message(chat_id: user.chat_id, text: 'Hello, world')
end

@telig26

This comment has been minimized.

Show comment
Hide comment
@telig26

telig26 Jul 22, 2018

No description provided.

telig26 commented Jul 22, 2018

No description provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment