telegram_bot 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +13 -0
- data/README.md +31 -16
- data/example/Gemfile +4 -0
- data/example/README.md +17 -0
- data/example/bot.rb +24 -0
- data/lib/telegram_bot/bot.rb +41 -16
- data/lib/telegram_bot/channel.rb +6 -0
- data/lib/telegram_bot/message.rb +2 -1
- data/lib/telegram_bot/null_logger.rb +11 -0
- data/lib/telegram_bot/out_message.rb +12 -1
- data/lib/telegram_bot/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0e96a036eb8842252f8905e9686702b902578c7
|
4
|
+
data.tar.gz: a20ea036287e3f2b25a14dcf7ef9950230b6f765
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f61ea704d8c9586a4792f3f579c8da67e65366ed1462bde4c8b24909cf31ac2f4d5ae1042c84877caa79ddcc2d529503a4afcd2e714ea212012d517c49c4be67
|
7
|
+
data.tar.gz: d9df7f17c4a725fb8b7f7bf7cc4f7c3e8c23cacc1c6756026107d31b6eb8a2e415a24c4474954b1e2a300b028f3991619215c615d48a6af69512f4911faac5f4
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# TelegramBot
|
2
2
|
|
3
|
-
A charismatic client for [Telegram's Bot API](https://core.telegram.org/bots).
|
3
|
+
A charismatic ruby client for [Telegram's Bot API](https://core.telegram.org/bots).
|
4
4
|
|
5
|
-
Write your own Telegram Bot!
|
5
|
+
Write your own Telegram Bot using Ruby! Yay!
|
6
6
|
|
7
7
|
Currently under heavy development.
|
8
|
-
|
8
|
+
Please [collaborate](https://github.com/eljojo/telegram_bot/issues/new) with your questions, ideas or problems!
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
@@ -19,40 +19,55 @@ And then execute:
|
|
19
19
|
|
20
20
|
$ bundle
|
21
21
|
|
22
|
-
Or install it yourself as:
|
23
|
-
|
24
|
-
$ gem install telegram_bot
|
25
|
-
|
26
22
|
## Usage
|
27
23
|
|
28
24
|
Here's an example:
|
29
25
|
|
30
26
|
```ruby
|
31
27
|
require 'telegram_bot'
|
32
|
-
require 'pp'
|
33
28
|
|
34
29
|
bot = TelegramBot.new('[YOUR TELEGRAM BOT TOKEN GOES HERE]')
|
35
30
|
bot.get_updates do |message|
|
36
|
-
|
31
|
+
puts "@#{message.from.username}: #{message.text}"
|
37
32
|
command = message.get_command_for(bot)
|
38
33
|
|
39
34
|
message.reply do |reply|
|
40
|
-
|
41
|
-
|
35
|
+
case command
|
36
|
+
when /greet/i
|
37
|
+
reply.text = "Hello, #{message.from.first_name}!"
|
38
|
+
else
|
39
|
+
reply.text = "#{message.from.first_name}, have no idea what #{command.inspect} means."
|
40
|
+
end
|
41
|
+
puts "sending #{reply.text.inspect} to @#{message.from.username}"
|
42
42
|
reply.send_with(bot)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
```
|
46
46
|
|
47
|
+
Here's a sample output:
|
48
|
+
|
49
|
+
```
|
50
|
+
$ bundle exec ruby bot.rb
|
51
|
+
@eljojo: greet
|
52
|
+
sending "Hello, José!" to @eljojo
|
53
|
+
@eljojo: heeeeeeeeya!
|
54
|
+
sending "José, have no idea what \"heeeeeeeeya!\" means." to @eljojo
|
55
|
+
```
|
56
|
+
|
57
|
+
![Example](http://i.imgur.com/VF8X4CQ.png)
|
58
|
+
|
47
59
|
## How do I get a Bot Token
|
48
60
|
|
49
61
|
Talk to the [@BotFather](https://telegram.me/botfather).
|
50
62
|
You can find more info [here](https://core.telegram.org/bots).
|
51
63
|
|
64
|
+
![How to get Token](http://i.imgur.com/90ya4Oe.png)
|
65
|
+
|
52
66
|
## Contributing
|
53
67
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/eljojo/telegram_bot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
69
|
+
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/example/Gemfile
ADDED
data/example/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
TelegramBot example
|
2
|
+
==================
|
3
|
+
|
4
|
+
To run this example type the following in a shell:
|
5
|
+
|
6
|
+
```
|
7
|
+
git clone https://github.com/eljojo/telegram_bot.git
|
8
|
+
cd telegram_bot/example
|
9
|
+
bundle install
|
10
|
+
```
|
11
|
+
|
12
|
+
Then, you can start the bot with:
|
13
|
+
```
|
14
|
+
bundle exec ruby bot.rb
|
15
|
+
```
|
16
|
+
|
17
|
+
You can close it with ``CTRL+C``
|
data/example/bot.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'telegram_bot'
|
2
|
+
require 'pp'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
logger = Logger.new(STDOUT, Logger::DEBUG)
|
6
|
+
|
7
|
+
bot = TelegramBot.new('YOUR KEY GOES HERE')
|
8
|
+
logger.debug "starting telegram bot"
|
9
|
+
|
10
|
+
bot.get_updates do |message|
|
11
|
+
logger.info "@#{message.from.username}: #{message.text}"
|
12
|
+
command = message.get_command_for(bot)
|
13
|
+
|
14
|
+
message.reply do |reply|
|
15
|
+
case command
|
16
|
+
when /greet/i
|
17
|
+
reply.text = "Hello, #{message.from.first_name}!"
|
18
|
+
else
|
19
|
+
reply.text = "#{message.from.first_name}, have no idea what #{command.inspect} means."
|
20
|
+
end
|
21
|
+
logger.info "sending #{reply.text.inspect} to @#{message.from.username}"
|
22
|
+
reply.send_with(bot)
|
23
|
+
end
|
24
|
+
end
|
data/lib/telegram_bot/bot.rb
CHANGED
@@ -2,45 +2,70 @@ module TelegramBot
|
|
2
2
|
class Bot
|
3
3
|
ENDPOINT = 'https://api.telegram.org/'
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(opts = {})
|
6
|
+
# compatibility with just passing a token
|
7
|
+
if opts.is_a?(String)
|
8
|
+
opts = { token: opts }
|
9
|
+
end
|
7
10
|
|
8
|
-
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
11
|
+
@token = opts.fetch(:token)
|
12
|
+
@timeout = opts[:timeout] || 50
|
13
|
+
@offset = opts[:offset] || 0
|
14
|
+
@logger = opts[:logger] || NullLoger.new
|
12
15
|
@connection = Excon.new(ENDPOINT, persistent: true)
|
13
|
-
@me = get_me
|
14
16
|
end
|
15
17
|
|
16
18
|
def get_me
|
17
|
-
|
18
|
-
|
19
|
+
@me ||= begin
|
20
|
+
response = request(:getMe)
|
21
|
+
User.new(response.result)
|
22
|
+
end
|
19
23
|
end
|
24
|
+
alias_method :me, :get_me
|
25
|
+
alias_method :identity, :me
|
20
26
|
|
21
|
-
def get_updates(&block)
|
27
|
+
def get_updates(opts = {}, &block)
|
28
|
+
return get_last_messages(opts) unless block_given?
|
29
|
+
|
30
|
+
logger.info "starting get_updates loop"
|
22
31
|
loop do
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
yield update.message
|
32
|
+
messages = get_last_messages(opts)
|
33
|
+
messages.each do |message|
|
34
|
+
logger.info "message from @#{message.chat.friendly_name}: #{message.text.inspect}"
|
35
|
+
yield message
|
28
36
|
end
|
29
37
|
end
|
30
38
|
end
|
31
39
|
|
32
40
|
def send_message(out_message)
|
33
41
|
response = request(:sendMessage, out_message.to_h)
|
42
|
+
logger.info "sending message: #{out_message.text.inspect} to #{out_message.chat_friendly_name}"
|
34
43
|
Message.new(response.result)
|
35
44
|
end
|
36
45
|
|
37
|
-
|
38
46
|
private
|
39
47
|
attr_reader :token
|
48
|
+
attr_reader :logger
|
49
|
+
|
40
50
|
def request(action, query = {})
|
41
51
|
path = "/bot#{@token}/#{action}"
|
42
52
|
res = @connection.post(path: path, query: query)
|
43
53
|
ApiResponse.new(res)
|
44
54
|
end
|
55
|
+
|
56
|
+
def get_last_updates(opts = {})
|
57
|
+
response = request(:getUpdates, offset: opts[:offset] || @offset, timeout: opts[:timeout] || @timeout)
|
58
|
+
if opts[:fail_silently]
|
59
|
+
logger.warn "error when getting updates. ignoring due to fail_silently."
|
60
|
+
return [] if !response.ok? || !response.result
|
61
|
+
end
|
62
|
+
updates = response.result.map{|raw_update| Update.new(raw_update) }
|
63
|
+
@offset = updates.last.id + 1 if updates.any?
|
64
|
+
updates
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_last_messages(opts = {})
|
68
|
+
get_last_updates(opts).map(&:message)
|
69
|
+
end
|
45
70
|
end
|
46
71
|
end
|
data/lib/telegram_bot/channel.rb
CHANGED
data/lib/telegram_bot/message.rb
CHANGED
@@ -5,13 +5,14 @@ module TelegramBot
|
|
5
5
|
alias_method :id, :message_id
|
6
6
|
alias_method :to_i, :id
|
7
7
|
attribute :from, User
|
8
|
+
alias_method :user, :from
|
8
9
|
attribute :text, String
|
9
10
|
attribute :date, DateTime
|
10
11
|
attribute :chat, Channel
|
11
12
|
attribute :reply_to_message, Message
|
12
13
|
|
13
14
|
def reply(&block)
|
14
|
-
reply = OutMessage.new(
|
15
|
+
reply = OutMessage.new(chat: chat)
|
15
16
|
yield reply if block_given?
|
16
17
|
reply
|
17
18
|
end
|
@@ -1,11 +1,22 @@
|
|
1
1
|
module TelegramBot
|
2
2
|
class OutMessage
|
3
3
|
include Virtus.model
|
4
|
-
attribute :
|
4
|
+
attribute :chat, Channel
|
5
5
|
attribute :text, String
|
6
6
|
|
7
7
|
def send_with(bot)
|
8
8
|
bot.send_message(self)
|
9
9
|
end
|
10
|
+
|
11
|
+
def chat_friendly_name
|
12
|
+
chat.friendly_name
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_h
|
16
|
+
{
|
17
|
+
text: text,
|
18
|
+
chat_id: chat.id
|
19
|
+
}
|
20
|
+
end
|
10
21
|
end
|
11
22
|
end
|
data/lib/telegram_bot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegram_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- José Tomás Albornoz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: excon
|
@@ -74,16 +74,21 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- CODE_OF_CONDUCT.md
|
77
78
|
- Gemfile
|
78
79
|
- LICENSE.txt
|
79
80
|
- README.md
|
80
81
|
- Rakefile
|
82
|
+
- example/Gemfile
|
83
|
+
- example/README.md
|
84
|
+
- example/bot.rb
|
81
85
|
- lib/telegram_bot.rb
|
82
86
|
- lib/telegram_bot/api_response.rb
|
83
87
|
- lib/telegram_bot/bot.rb
|
84
88
|
- lib/telegram_bot/channel.rb
|
85
89
|
- lib/telegram_bot/group_chat.rb
|
86
90
|
- lib/telegram_bot/message.rb
|
91
|
+
- lib/telegram_bot/null_logger.rb
|
87
92
|
- lib/telegram_bot/out_message.rb
|
88
93
|
- lib/telegram_bot/update.rb
|
89
94
|
- lib/telegram_bot/user.rb
|