Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

how to send a personal message to a user? #5

Open
icy opened this issue Jul 10, 2015 · 3 comments
Open

how to send a personal message to a user? #5

icy opened this issue Jul 10, 2015 · 3 comments

Comments

@icy
Copy link
Contributor

icy commented Jul 10, 2015

I have a bot that will send personal message (PM) to the user who has invoked a bot command on the group. How to get this?

Thanks.

@dkam
Copy link

dkam commented Sep 13, 2015

I did it like this, but I'd like to see an example in the README, because I've no idea if this is how it should be done. The user_name, user_id variables come from a previous interaction.

bot = TelegramBot.new(token: TELEGRAPH_KEY)

c = TelegramBot::Channel.new
c.username = user_name
c.id = user_id

o = TelegramBot::OutMessage.new
o.chat = c
o.text = "Hello"
o.send_with(bot)

@icy
Copy link
Contributor Author

icy commented Sep 14, 2015

Thanks a lot, @dkam !

@eljojo
Copy link
Owner

eljojo commented Sep 14, 2015

hi everyone, I am really sorry I have been super busy.
I also haven't had the chance to play recently with telegram's API, so I haven't been following so closely the newest developments.

The username is not needed, so you can do just this:

bot = TelegramBot.new(token: TELEGRAM_KEY)

channel = TelegramBot::Channel.new(id: user_id)

o = TelegramBot::OutMessage.new
o.chat = channel
o.text = "Hello"
o.send_with(bot)

Because Users respond to #id, and in Telegram, a user id is also a valid channel, you can also do the following:

user = TelegramBot::User.new(id: user_id)

out_message = TelegramBot::OutMessage.new
out_message.chat = user
out_message.text = "Hello"
out_message.send_with(bot)

I was just going through the code and GroupChat has no method that returns you some array with all the Users.
Maybe it'd be useful to have GroupChat returning an array with all the users, and also in User, defining a method with some nice DSL along the lines of:

bot.get_updates do |message|
  if message.group_chat?
    channel_members = message.chat.users # array of User
    user = channel_members.first
    user.new_message do |answer|
      answer.text = "Hello #{user.first_name}"
      answer.send_with(bot)
    end
  end
end

Any ideas?

rozhok added a commit to rozhok/telegram_bot that referenced this issue Nov 22, 2017
Add exmaple of sending messages to README.
Add IntelliJ project files to .gitignore.
eljojo added a commit that referenced this issue Jun 9, 2018
#5 Add send message instructions to README
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants