ya_telegram_bot 0.2.0 → 0.3.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c607a290b4ddc0b1aa0e6655266736dfc049db55
|
4
|
+
data.tar.gz: d5de93cfa149d2e35f0f46053117335fdc3d6efd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a72d2a026a4bbe3e6d12f4e77db06a7b249df924236c42aed30dbdd60700e86a7ff46a9cff8ae7d69c7c24aff60b8343d044cd25f907f0708b360c84bd2aeda7
|
7
|
+
data.tar.gz: dadfbb256be7c04a62dbb82f61710564b861a7da99ec8d2695bd3f94e29c5666ac7cf67e198e6476ae8677f4d4be5b341d52461b3abc4698cd5b8a6b5d26d40d
|
data/README.md
CHANGED
@@ -2,11 +2,10 @@
|
|
2
2
|
|
3
3
|
**IN DEV**
|
4
4
|
|
5
|
-
## What's new **0.
|
6
|
-
|
7
|
-
* Class `YATelegramBot::TelegramAPI::Message`.
|
8
|
-
* `Base#Update` generates array of `Message`. See Usage.
|
5
|
+
## What's new **0.3.0**
|
9
6
|
|
7
|
+
* Class `YATelegramBot::TelegramAPI::User`.
|
8
|
+
* Class `YATelegramBot::TelegramAPI::Chat`.
|
10
9
|
|
11
10
|
## Usage
|
12
11
|
|
@@ -19,6 +18,8 @@ class Bot
|
|
19
18
|
token YOUR_TOKEN
|
20
19
|
end
|
21
20
|
|
21
|
+
# getting LAST updates as array of YATelegramBot:TelegramAPI::Message
|
22
|
+
# "LAST" means that #updates method every time will return fresh updates (so you will never get same updates inside your process)
|
22
23
|
updates = Bot.updates
|
23
24
|
updates.each do |message|
|
24
25
|
chat_id = message['chat']['id']
|
@@ -30,8 +31,18 @@ updates.each do |message|
|
|
30
31
|
markdown: true
|
31
32
|
end
|
32
33
|
|
34
|
+
# reply to message
|
33
35
|
Bot.updates.each { |message| message.reply text: 'Leave me alone!' }
|
34
36
|
|
37
|
+
# YATelegramBot::TelegramAPI::User
|
38
|
+
user = Bot.updates[0].from
|
39
|
+
user.send_text text: '*hey hey!*',
|
40
|
+
markdown: true
|
41
|
+
|
42
|
+
#YATelegramBot::TelegramAPI::Chat
|
43
|
+
chat = Bot.updates[0].chat
|
44
|
+
chat.send_text text: "Hi, #{chat.type == :private ? 'dude' : 'all'}!"
|
45
|
+
|
35
46
|
```
|
36
47
|
|
37
48
|
## Contributing
|
data/lib/ya_telegram_bot/base.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
module YATelegramBot
|
2
|
+
module TelegramAPI
|
3
|
+
#
|
4
|
+
# represents Telegram Chat
|
5
|
+
#
|
6
|
+
class Chat < Hash
|
7
|
+
FIELDS = [:id, :type, :title, :first_name, :last_name, :username].freeze
|
8
|
+
|
9
|
+
#
|
10
|
+
# @param hash [Hash] hash from json.
|
11
|
+
# @param bot [Base] for using api methods
|
12
|
+
#
|
13
|
+
def initialize(hash, bot = nil)
|
14
|
+
super()
|
15
|
+
@bot = bot
|
16
|
+
FIELDS.each { |f| self[f] = hash[f.to_s] if hash[f.to_s] }
|
17
|
+
|
18
|
+
self[:type] = self[:type].to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# @example chat.type
|
23
|
+
#
|
24
|
+
def method_missing(m, *args)
|
25
|
+
return self[m] if self[m] && args.empty?
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# uses bot to send text to this user.
|
31
|
+
#
|
32
|
+
# @param params [Hash] params for Base#send_text. This method will only set :chat to self[:id]
|
33
|
+
#
|
34
|
+
def send_text(params = {})
|
35
|
+
fail InitWithoutBot unless @bot
|
36
|
+
|
37
|
+
params[:chat] = id
|
38
|
+
@bot.send_text params
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'time'
|
2
|
+
require_relative 'user'
|
3
|
+
require_relative 'chat'
|
2
4
|
|
3
5
|
module YATelegramBot
|
4
6
|
module TelegramAPI
|
@@ -45,7 +47,7 @@ module YATelegramBot
|
|
45
47
|
#
|
46
48
|
# params values:
|
47
49
|
# * :text [String]
|
48
|
-
# * :as_plain_message [Boolean] default: true. If it's
|
50
|
+
# * :as_plain_message [Boolean] default: true. If it's true, method won't set :reply_to parameter
|
49
51
|
#
|
50
52
|
# @example message.reply(text: 'Hi, *friend*!', markdown: true)
|
51
53
|
#
|
@@ -68,11 +70,10 @@ module YATelegramBot
|
|
68
70
|
# @return [Hash]
|
69
71
|
#
|
70
72
|
def hash_for_merging(hash)
|
71
|
-
new_hash = {
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
new_hash[:chat] = hash['chat'] # TODO: class Chat
|
73
|
+
new_hash = { id: hash['message_id'].to_i,
|
74
|
+
date: Time.at(hash['date'].to_i),
|
75
|
+
from: User.new(hash['from'], @bot),
|
76
|
+
chat: Chat.new(hash['chat'], @bot) }
|
76
77
|
|
77
78
|
type = TYPES.find { |t| hash[t.to_s] }
|
78
79
|
new_hash[type] = hash[type.to_s] # TODO: fail if type not found
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module YATelegramBot
|
2
|
+
module TelegramAPI
|
3
|
+
#
|
4
|
+
# represents Telegram User
|
5
|
+
#
|
6
|
+
class User < Hash
|
7
|
+
FIELDS = [:id, :first_name, :last_name, :username].freeze
|
8
|
+
|
9
|
+
#
|
10
|
+
# @param hash [Hash] hash from json.
|
11
|
+
# @param bot [Base] for using api methods
|
12
|
+
#
|
13
|
+
def initialize(hash, bot = nil)
|
14
|
+
super()
|
15
|
+
@bot = bot
|
16
|
+
FIELDS.each { |f| self[f] = hash[f.to_s] if hash[f.to_s] }
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# @example user.first_name
|
21
|
+
#
|
22
|
+
def method_missing(m, *args)
|
23
|
+
return self[m] if self[m] && args.empty?
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# uses bot to send text to this user.
|
29
|
+
#
|
30
|
+
# @param params [Hash] params for Base#send_text. This method will only set :chat to self[:id]
|
31
|
+
#
|
32
|
+
def send_text(params = {})
|
33
|
+
fail InitWithoutBot unless @bot
|
34
|
+
|
35
|
+
params[:chat] = id
|
36
|
+
@bot.send_text params
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ya_telegram_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitriy Non
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -99,7 +99,9 @@ files:
|
|
99
99
|
- lib/ya_telegram_bot.rb
|
100
100
|
- lib/ya_telegram_bot/base.rb
|
101
101
|
- lib/ya_telegram_bot/exceptions.rb
|
102
|
+
- lib/ya_telegram_bot/telegram_api/chat.rb
|
102
103
|
- lib/ya_telegram_bot/telegram_api/message.rb
|
104
|
+
- lib/ya_telegram_bot/telegram_api/user.rb
|
103
105
|
- lib/ya_telegram_bot/version.rb
|
104
106
|
- ya_telegram_bot.gemspec
|
105
107
|
homepage: https://github.com/Nondv/telegram_bot
|