telegram_bot 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0e96a036eb8842252f8905e9686702b902578c7
4
- data.tar.gz: a20ea036287e3f2b25a14dcf7ef9950230b6f765
3
+ metadata.gz: 346a299cac50693e640fa05f84230a3b287b8d5e
4
+ data.tar.gz: 72b1b56a15773f289c294f7c511e75feb5e3560d
5
5
  SHA512:
6
- metadata.gz: f61ea704d8c9586a4792f3f579c8da67e65366ed1462bde4c8b24909cf31ac2f4d5ae1042c84877caa79ddcc2d529503a4afcd2e714ea212012d517c49c4be67
7
- data.tar.gz: d9df7f17c4a725fb8b7f7bf7cc4f7c3e8c23cacc1c6756026107d31b6eb8a2e415a24c4474954b1e2a300b028f3991619215c615d48a6af69512f4911faac5f4
6
+ metadata.gz: 7bae1c4f717dfdb9f858ef37f14b96c2b09f4386eea894d4b204188cc1dc00c597d5daf32465356f16155fa105343d89ce7a63a4c32b01e730c58251a11fbe9e
7
+ data.tar.gz: 38a2b016b63e6a24f3d5a2b608dfde18f5d91662d478e3de3fc15f6095215e20be60e844dd3b7fc1335b376deb404c128031281467c00d0c41922f184a36fd4c
data/README.md CHANGED
@@ -12,7 +12,7 @@ Please [collaborate](https://github.com/eljojo/telegram_bot/issues/new) with you
12
12
  Add this line to your application's Gemfile (currently under development):
13
13
 
14
14
  ```ruby
15
- gem 'telegram_bot', github: 'eljojo/telegram_bot'
15
+ gem 'telegram_bot'
16
16
  ```
17
17
 
18
18
  And then execute:
@@ -26,8 +26,8 @@ Here's an example:
26
26
  ```ruby
27
27
  require 'telegram_bot'
28
28
 
29
- bot = TelegramBot.new('[YOUR TELEGRAM BOT TOKEN GOES HERE]')
30
- bot.get_updates do |message|
29
+ bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]')
30
+ bot.get_updates(fail_silently: true) do |message|
31
31
  puts "@#{message.from.username}: #{message.text}"
32
32
  command = message.get_command_for(bot)
33
33
 
@@ -63,6 +63,53 @@ You can find more info [here](https://core.telegram.org/bots).
63
63
 
64
64
  ![How to get Token](http://i.imgur.com/90ya4Oe.png)
65
65
 
66
+ ## What else can it do?
67
+
68
+ you can pass options to the bot initializer:
69
+ ```ruby
70
+ bot = TelegramBot.new(token: 'abc', logger: Logger.new(STDOUT), offset: 123, timeout: 20)
71
+ ```
72
+
73
+ if you don't want to start the loop, don't pass a block to ``#get_updates`` and you'll get an array with the latest messages:
74
+ ```ruby
75
+ messages = bot.get_updates(timeout: 30, offset: 123)
76
+ ```
77
+
78
+ Because things can go wrong sometimes with the API, there's a ``fail_silently`` option that you can pass to ``#get_updates`` like this:
79
+ ```ruby
80
+ bot.get_updates(fail_silently: true) do |message|
81
+ puts message.text
82
+ end
83
+ ```
84
+
85
+ A message has several attributes:
86
+ ```ruby
87
+ message = bot.get_updates.last
88
+
89
+ # message data
90
+ message.text # "hello moto"
91
+ message.date # Wed, 01 Jul 2015 09:52:54 +0200 (DateTime)
92
+
93
+ # reading user
94
+ message.from # TelegramBot::User
95
+ message.from.first_name # "Homer"
96
+ message.from.last_name # "Simpson"
97
+ message.from.username # "mr_x"
98
+
99
+ # channel
100
+ message.channel.id # 123123123 (telegram's id)
101
+
102
+ # reply
103
+ message.reply do |reply|
104
+ reply.text = "homer please clean the garage"
105
+ reply.send_with(bot)
106
+ end
107
+ # or
108
+ reply = message.reply
109
+ reply.text = "i'll do it after going to moe's"
110
+ bot.send_message(reply)
111
+ ```
112
+
66
113
  ## Contributing
67
114
 
68
115
  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.
@@ -4,10 +4,10 @@ require 'logger'
4
4
 
5
5
  logger = Logger.new(STDOUT, Logger::DEBUG)
6
6
 
7
- bot = TelegramBot.new('YOUR KEY GOES HERE')
7
+ bot = TelegramBot.new(token: 'YOUR KEY GOES HERE', logger: logger)
8
8
  logger.debug "starting telegram bot"
9
9
 
10
- bot.get_updates do |message|
10
+ bot.get_updates(fail_silently: true) do |message|
11
11
  logger.info "@#{message.from.username}: #{message.text}"
12
12
  command = message.get_command_for(bot)
13
13
 
@@ -55,9 +55,9 @@ module TelegramBot
55
55
 
56
56
  def get_last_updates(opts = {})
57
57
  response = request(:getUpdates, offset: opts[:offset] || @offset, timeout: opts[:timeout] || @timeout)
58
- if opts[:fail_silently]
58
+ if opts[:fail_silently] && (!response.ok? || !response.result)
59
59
  logger.warn "error when getting updates. ignoring due to fail_silently."
60
- return [] if !response.ok? || !response.result
60
+ return []
61
61
  end
62
62
  updates = response.result.map{|raw_update| Update.new(raw_update) }
63
63
  @offset = updates.last.id + 1 if updates.any?
@@ -1,3 +1,3 @@
1
1
  module TelegramBot
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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.2
4
+ version: 0.0.3
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-30 00:00:00.000000000 Z
11
+ date: 2015-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon