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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a92c64ad73728e0f777bf7d403e6bb62f13d698
4
- data.tar.gz: 96e0b8b4ca3d224c6c263ea9ad1b056e9c01c683
3
+ metadata.gz: b0e96a036eb8842252f8905e9686702b902578c7
4
+ data.tar.gz: a20ea036287e3f2b25a14dcf7ef9950230b6f765
5
5
  SHA512:
6
- metadata.gz: 317077a37c2a9fbb4fe2fb7e8cddb0dcf4005563e4648b882b0f6e06f04756794b5fe480f0bed7540b4d2f665d26d8e6bdf17d402d3defa519c0b1ecd9656597
7
- data.tar.gz: 24581019d494cfc0ac94754b77b6efc2d0bd910e749f16b0aacba6c09fedbaad8595aa17b7d897612599d4b86d33459f8d053eeebc8a9aec200893d7879a0306
6
+ metadata.gz: f61ea704d8c9586a4792f3f579c8da67e65366ed1462bde4c8b24909cf31ac2f4d5ae1042c84877caa79ddcc2d529503a4afcd2e714ea212012d517c49c4be67
7
+ data.tar.gz: d9df7f17c4a725fb8b7f7bf7cc4f7c3e8c23cacc1c6756026107d31b6eb8a2e415a24c4474954b1e2a300b028f3991619215c615d48a6af69512f4911faac5f4
@@ -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
- Contributions are always welcome.
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
- pp(message)
31
+ puts "@#{message.from.username}: #{message.text}"
37
32
  command = message.get_command_for(bot)
38
33
 
39
34
  message.reply do |reply|
40
- reply.text = "i think that #{command.inspect} is false"
41
- pp(reply)
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
- 1. Fork it ( https://github.com/eljojo/telegram_bot/fork )
55
- 2. Create your feature branch (`git checkout -b my-new-feature`)
56
- 3. Commit your changes (`git commit -am 'Add some feature'`)
57
- 4. Push to the branch (`git push origin my-new-feature`)
58
- 5. Create a new Pull Request
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).
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gem "telegram_bot", github: 'eljojo/telegram_bot'
@@ -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``
@@ -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
@@ -2,45 +2,70 @@ module TelegramBot
2
2
  class Bot
3
3
  ENDPOINT = 'https://api.telegram.org/'
4
4
 
5
- attr_reader :me
6
- alias_method :identity, :me
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
- def initialize(token)
9
- @token = token
10
- @timeout = 50
11
- @offset = 0
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
- response = request(:getMe)
18
- User.new(response.result)
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
- response = request(:getUpdates, offset: @offset, timeout: @timeout)
24
- response.result.each do |raw_update|
25
- update = Update.new(raw_update)
26
- @offset = update.id + 1
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
@@ -2,5 +2,11 @@ module TelegramBot
2
2
  class Channel
3
3
  include Virtus.model
4
4
  attribute :id, Integer
5
+ attribute :username, String
6
+ attribute :title, String
7
+
8
+ def friendly_name
9
+ username ? "@#{username}" : "channel #{title.inspect}"
10
+ end
5
11
  end
6
12
  end
@@ -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(chat_id: chat.id)
15
+ reply = OutMessage.new(chat: chat)
15
16
  yield reply if block_given?
16
17
  reply
17
18
  end
@@ -0,0 +1,11 @@
1
+ require 'logger'
2
+
3
+ module TelegramBot
4
+ class NullLoger < Logger
5
+ def initialize(*args)
6
+ end
7
+
8
+ def add(*args, &block)
9
+ end
10
+ end
11
+ end
@@ -1,11 +1,22 @@
1
1
  module TelegramBot
2
2
  class OutMessage
3
3
  include Virtus.model
4
- attribute :chat_id, Integer
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
@@ -1,3 +1,3 @@
1
1
  module TelegramBot
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
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.1
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-24 00:00:00.000000000 Z
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