fantastic_robot 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +117 -0
- data/.ruby-version +1 -0
- data/.travis.yml +19 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +48 -0
- data/LICENSE +22 -0
- data/README.md +147 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fantastic_robot.gemspec +32 -0
- data/lib/fantastic_robot/configuration.rb +32 -0
- data/lib/fantastic_robot/connection.rb +36 -0
- data/lib/fantastic_robot/model/attachment/audio.rb +12 -0
- data/lib/fantastic_robot/model/attachment/base.rb +15 -0
- data/lib/fantastic_robot/model/attachment/contact.rb +11 -0
- data/lib/fantastic_robot/model/attachment/document.rb +16 -0
- data/lib/fantastic_robot/model/attachment/location.rb +10 -0
- data/lib/fantastic_robot/model/attachment/photo_size.rb +12 -0
- data/lib/fantastic_robot/model/attachment/sticker.rb +17 -0
- data/lib/fantastic_robot/model/attachment/video.rb +17 -0
- data/lib/fantastic_robot/model/attachment/voice.rb +12 -0
- data/lib/fantastic_robot/model/base.rb +68 -0
- data/lib/fantastic_robot/model/chat.rb +14 -0
- data/lib/fantastic_robot/model/file.rb +14 -0
- data/lib/fantastic_robot/model/message.rb +51 -0
- data/lib/fantastic_robot/model/update.rb +15 -0
- data/lib/fantastic_robot/model/user.rb +11 -0
- data/lib/fantastic_robot/models.rb +6 -0
- data/lib/fantastic_robot/request/base.rb +59 -0
- data/lib/fantastic_robot/request/forward_message.rb +18 -0
- data/lib/fantastic_robot/request/get_me.rb +9 -0
- data/lib/fantastic_robot/request/send_audio.rb +32 -0
- data/lib/fantastic_robot/request/send_chat_action.rb +18 -0
- data/lib/fantastic_robot/request/send_document.rb +32 -0
- data/lib/fantastic_robot/request/send_location.rb +17 -0
- data/lib/fantastic_robot/request/send_message.rb +17 -0
- data/lib/fantastic_robot/request/send_photo.rb +16 -0
- data/lib/fantastic_robot/request/send_sticker.rb +18 -0
- data/lib/fantastic_robot/request/send_video.rb +32 -0
- data/lib/fantastic_robot/request/send_voice.rb +32 -0
- data/lib/fantastic_robot/request/set_webhook.rb +29 -0
- data/lib/fantastic_robot/requests.rb +13 -0
- data/lib/fantastic_robot/version.rb +3 -0
- data/lib/fantastic_robot.rb +56 -0
- data/lib/generators/fantastic_robot/files/fantastic_robot.rb +12 -0
- data/lib/generators/fantastic_robot/install_generator.rb +13 -0
- metadata +197 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 70e7924cf945c7d75a688d2e6ddd86b8e4214978
|
4
|
+
data.tar.gz: 6a2385d639bb2a72ab956396fa68a633b5465185
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 99031d282478178e085e21cea7e3fb5551d536c140da3c5ea7a03e5efb29e68e1a4f7be42c199c83c9323d661c396b6d78e72d4e2e468e004ff12bc430007365
|
7
|
+
data.tar.gz: f353a05df9aa13c2646fa3ffa159427c9317040c24932e72e60b4b804490eeb8566a1a0b89025440bc09f4fe0ff0c43aa4108f5c32bbfbb3f35052ca65b3f957
|
data/.gitignore
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
|
2
|
+
# Created by https://www.gitignore.io/api/windows,linux,osx,sublimetext,ruby
|
3
|
+
|
4
|
+
### Windows ###
|
5
|
+
# Windows image file caches
|
6
|
+
Thumbs.db
|
7
|
+
ehthumbs.db
|
8
|
+
|
9
|
+
# Folder config file
|
10
|
+
Desktop.ini
|
11
|
+
|
12
|
+
# Recycle Bin used on file shares
|
13
|
+
$RECYCLE.BIN/
|
14
|
+
|
15
|
+
# Windows Installer files
|
16
|
+
*.cab
|
17
|
+
*.msi
|
18
|
+
*.msm
|
19
|
+
*.msp
|
20
|
+
|
21
|
+
# Windows shortcuts
|
22
|
+
*.lnk
|
23
|
+
|
24
|
+
|
25
|
+
### Linux ###
|
26
|
+
*~
|
27
|
+
|
28
|
+
# KDE directory preferences
|
29
|
+
.directory
|
30
|
+
|
31
|
+
# Linux trash folder which might appear on any partition or disk
|
32
|
+
.Trash-*
|
33
|
+
|
34
|
+
|
35
|
+
### OSX ###
|
36
|
+
.DS_Store
|
37
|
+
.AppleDouble
|
38
|
+
.LSOverride
|
39
|
+
|
40
|
+
# Icon must end with two \r
|
41
|
+
Icon
|
42
|
+
|
43
|
+
|
44
|
+
# Thumbnails
|
45
|
+
._*
|
46
|
+
|
47
|
+
# Files that might appear in the root of a volume
|
48
|
+
.DocumentRevisions-V100
|
49
|
+
.fseventsd
|
50
|
+
.Spotlight-V100
|
51
|
+
.TemporaryItems
|
52
|
+
.Trashes
|
53
|
+
.VolumeIcon.icns
|
54
|
+
|
55
|
+
# Directories potentially created on remote AFP share
|
56
|
+
.AppleDB
|
57
|
+
.AppleDesktop
|
58
|
+
Network Trash Folder
|
59
|
+
Temporary Items
|
60
|
+
.apdisk
|
61
|
+
|
62
|
+
|
63
|
+
### SublimeText ###
|
64
|
+
# cache files for sublime text
|
65
|
+
*.tmlanguage.cache
|
66
|
+
*.tmPreferences.cache
|
67
|
+
*.stTheme.cache
|
68
|
+
|
69
|
+
# workspace files are user-specific
|
70
|
+
*.sublime-workspace
|
71
|
+
|
72
|
+
# project files should be checked into the repository, unless a significant
|
73
|
+
# proportion of contributors will probably not be using SublimeText
|
74
|
+
# *.sublime-project
|
75
|
+
|
76
|
+
# sftp configuration file
|
77
|
+
sftp-config.json
|
78
|
+
|
79
|
+
|
80
|
+
### Ruby ###
|
81
|
+
*.gem
|
82
|
+
*.rbc
|
83
|
+
/.config
|
84
|
+
/coverage/
|
85
|
+
/InstalledFiles
|
86
|
+
/pkg/
|
87
|
+
/spec/reports/
|
88
|
+
/spec/examples.txt
|
89
|
+
/test/tmp/
|
90
|
+
/test/version_tmp/
|
91
|
+
/tmp/
|
92
|
+
|
93
|
+
## Specific to RubyMotion:
|
94
|
+
.dat*
|
95
|
+
.repl_history
|
96
|
+
build/
|
97
|
+
|
98
|
+
## Documentation cache and generated files:
|
99
|
+
/.yardoc/
|
100
|
+
/_yardoc/
|
101
|
+
/doc/
|
102
|
+
/rdoc/
|
103
|
+
|
104
|
+
## Environment normalization:
|
105
|
+
/.bundle/
|
106
|
+
/vendor/bundle
|
107
|
+
/lib/bundler/man/
|
108
|
+
|
109
|
+
# for a library or gem, you might want to ignore these files since the code is
|
110
|
+
# intended to run in multiple environments; otherwise, check them in:
|
111
|
+
# Gemfile.lock
|
112
|
+
# .ruby-version
|
113
|
+
# .ruby-gemset
|
114
|
+
|
115
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
116
|
+
.rvmrc
|
117
|
+
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/.travis.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0.0
|
4
|
+
- 2.1.0
|
5
|
+
- 2.2.0
|
6
|
+
- 2.2.3
|
7
|
+
|
8
|
+
before_install:
|
9
|
+
- gem update --system
|
10
|
+
- gem --version
|
11
|
+
- gem install bundler
|
12
|
+
- bundler --version
|
13
|
+
|
14
|
+
script: bundle exec rake test
|
15
|
+
|
16
|
+
branches:
|
17
|
+
only:
|
18
|
+
- master
|
19
|
+
- development
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at oscar.dearriba@the-cocktail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fantastic_robot (0.1.1)
|
5
|
+
activemodel (>= 4.0.0, < 5.0.0)
|
6
|
+
faraday (~> 0.9.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (4.2.5)
|
12
|
+
activesupport (= 4.2.5)
|
13
|
+
builder (~> 3.1)
|
14
|
+
activesupport (4.2.5)
|
15
|
+
i18n (~> 0.7)
|
16
|
+
json (~> 1.7, >= 1.7.7)
|
17
|
+
minitest (~> 5.1)
|
18
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
builder (3.2.2)
|
21
|
+
factory_girl (4.5.0)
|
22
|
+
activesupport (>= 3.0.0)
|
23
|
+
faker (1.6.1)
|
24
|
+
i18n (~> 0.5)
|
25
|
+
faraday (0.9.2)
|
26
|
+
multipart-post (>= 1.2, < 3)
|
27
|
+
i18n (0.7.0)
|
28
|
+
json (1.8.3)
|
29
|
+
minitest (5.8.3)
|
30
|
+
multipart-post (2.0.0)
|
31
|
+
rake (10.4.2)
|
32
|
+
thread_safe (0.3.5)
|
33
|
+
tzinfo (1.2.2)
|
34
|
+
thread_safe (~> 0.1)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
bundler (~> 1.10)
|
41
|
+
factory_girl (~> 4.0)
|
42
|
+
faker (~> 1.6)
|
43
|
+
fantastic_robot!
|
44
|
+
minitest (~> 5.0)
|
45
|
+
rake (~> 10.0)
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
1.11.2
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Óscar de Arriba
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
# fantastic_robot
|
2
|
+
[![Build Status](https://travis-ci.org/odarriba/fantastic_robot.svg)](https://travis-ci.org/odarriba/fantastic_robot)
|
3
|
+
|
4
|
+
The aim of this gem is to provide a useful wrapper of Telegram's Bot API that makes use of Webhooks functionality.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'fantastic_robot'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```
|
21
|
+
$ gem install fantastic_robot
|
22
|
+
```
|
23
|
+
|
24
|
+
## Configuration
|
25
|
+
|
26
|
+
Gem configuration can be initialized by running a custom generator:
|
27
|
+
|
28
|
+
```
|
29
|
+
$ rails g fantastic_robot:install
|
30
|
+
```
|
31
|
+
|
32
|
+
Then, you can change the configuration by editing the file located at `config/initializers/fantastic_robot.rb`
|
33
|
+
|
34
|
+
At least, you should include:
|
35
|
+
* Your bot's token
|
36
|
+
* Your web hook callback URL (in which you'll receive the updates sent to your bot).
|
37
|
+
|
38
|
+
**Important:** your webhook we'll be registered only if the `delivery_method` is set to `:delivery_method`.
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
This gem provides models to access easily to API updates, and to create responses and requests to send messages and attachments to Telegram.
|
43
|
+
|
44
|
+
### Models
|
45
|
+
|
46
|
+
Used to parse received data from Telegram.
|
47
|
+
|
48
|
+
Available models are:
|
49
|
+
* FantasticRobot::Model::Chat
|
50
|
+
* FantasticRobot::Model::Message
|
51
|
+
* FantasticRobot::Model::Update
|
52
|
+
* FantasticRobot::Model::User
|
53
|
+
* FantasticRobot::Model::Attachment::Audio
|
54
|
+
* FantasticRobot::Model::Attachment::Contact
|
55
|
+
* FantasticRobot::Model::Attachment::Document
|
56
|
+
* FantasticRobot::Model::Attachment::Location
|
57
|
+
* FantasticRobot::Model::Attachment::PhotoSize
|
58
|
+
* FantasticRobot::Model::Attachment::Sticker
|
59
|
+
* FantasticRobot::Model::Attachment::Video
|
60
|
+
* FantasticRobot::Model::Attachment::Voice
|
61
|
+
|
62
|
+
If you want to know the variables available in each one, you can check the [official docs](https://core.telegram.org/bots/api#available-types).
|
63
|
+
|
64
|
+
And they can be instantiated like any other model. You will commonly receive an `Update` object:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
received_hash = JSON.parse(request.body.read)
|
68
|
+
received_update = FantasticRobot::Model::Update.new(received_hash)
|
69
|
+
```
|
70
|
+
|
71
|
+
Associated models (like the `Message` included inside the `Update`) will be automatically converted when instantiating the `Update` object.
|
72
|
+
|
73
|
+
### Requests
|
74
|
+
|
75
|
+
Request are objects that can receive (and validate) the parameters that can be sent in a request to the API.
|
76
|
+
|
77
|
+
The available requests are:
|
78
|
+
* FantasticRobot::Request::ForwardMessage
|
79
|
+
* FantasticRobot::Request::GetMe
|
80
|
+
* FantasticRobot::Request::SendAudio
|
81
|
+
* FantasticRobot::Request::SendChatAction
|
82
|
+
* FantasticRobot::Request::SendDocument
|
83
|
+
* FantasticRobot::Request::SendLocation
|
84
|
+
* FantasticRobot::Request::SendMessage
|
85
|
+
* FantasticRobot::Request::SendPhoto
|
86
|
+
* FantasticRobot::Request::SendSticker
|
87
|
+
* FantasticRobot::Request::SendVideo
|
88
|
+
* FantasticRobot::Request::SendVoice
|
89
|
+
* FantasticRobot::Request::SetWebhook
|
90
|
+
|
91
|
+
If you want to know the variables available in each one, you can check the [official docs](https://core.telegram.org/bots/api#available-methods).
|
92
|
+
|
93
|
+
#### Sending a request
|
94
|
+
|
95
|
+
There are two ways of sending a request:
|
96
|
+
|
97
|
+
* All of them can be send asynchronously to the API using the `#send_request` method available in all request classes:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
req = FantasticRobot::Request::GetMe.new()
|
101
|
+
response = req.send_request
|
102
|
+
```
|
103
|
+
|
104
|
+
*Note:* if the API returns an error, a `Faraday::Error` exception will be raised
|
105
|
+
|
106
|
+
* Synchronously as the response to the updates reception in the webhook callback URL. For example:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
def receive
|
110
|
+
received = FantasticRobot::Model::Update.new(JSON.parse(request.body.read))
|
111
|
+
|
112
|
+
response = FantasticRobot::Request::SendMessage.new({
|
113
|
+
chat_id: received.message.chat.id,
|
114
|
+
text: "Okay! I've got it!",
|
115
|
+
reply_to_message_id: received.message.message_id
|
116
|
+
})
|
117
|
+
|
118
|
+
render json: response
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
## Unit testing
|
123
|
+
|
124
|
+
Several tests have been implemented to verify that model an requests behave as expected.
|
125
|
+
|
126
|
+
So please, if you want to contribute to this gem, check that tests are maintained and all of them pass.
|
127
|
+
|
128
|
+
## To-Do
|
129
|
+
|
130
|
+
There are several thing to do on this project:
|
131
|
+
* Receiving updates by polling.
|
132
|
+
* Getting user profile photos from the API.
|
133
|
+
* Create models of custom keyboard responses (actually they can be hardcoded as a Hash object, [check the documentation](https://core.telegram.org/bots/api#replykeyboardmarkup)).
|
134
|
+
* Inline queries.
|
135
|
+
|
136
|
+
If you want to check out the gem's source code and want to contribute, you are welcome!
|
137
|
+
|
138
|
+
## Contributing
|
139
|
+
|
140
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/odarriba/fantastic_robot](https://github.com/odarriba/fantastic_robot). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
141
|
+
|
142
|
+
Everyone's contributions are welcome, so if you want to contribute, just fork this repo, make the changes you want and create a descriptive pull request :)
|
143
|
+
|
144
|
+
|
145
|
+
## License
|
146
|
+
|
147
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT) as can be seen in LICENSE file.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "telegram/bot"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fantastic_robot/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "fantastic_robot"
|
8
|
+
s.version = FantasticRobot::VERSION
|
9
|
+
s.authors = ["Óscar de Arriba"]
|
10
|
+
s.email = ["odarriba@gmail.com"]
|
11
|
+
|
12
|
+
s.summary = %q{Ruby wrapper of Telegram's Bot API with web hooks functionality}
|
13
|
+
s.description = %q{Ruby wrapper of Telegram's Bot API to create applications using web hooks functionality.}
|
14
|
+
s.homepage = "https://github.com/odarriba/fantastic_robot"
|
15
|
+
s.license = "MIT"
|
16
|
+
|
17
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
s.bindir = "exe"
|
19
|
+
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.required_ruby_version = '>= 2.0.0'
|
23
|
+
|
24
|
+
s.add_dependency "activemodel", '>= 4.0.0', '< 5.0.0'
|
25
|
+
s.add_dependency "faraday", "~> 0.9.2"
|
26
|
+
|
27
|
+
s.add_development_dependency "bundler", "~> 1.10"
|
28
|
+
s.add_development_dependency "rake", "~> 10.0"
|
29
|
+
s.add_development_dependency "minitest", "~> 5.0"
|
30
|
+
s.add_development_dependency "factory_girl", "~> 4.0"
|
31
|
+
s.add_development_dependency "faker", "~> 1.6"
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module FantasticRobot
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :api_key
|
6
|
+
attr_accessor :delivery_method
|
7
|
+
attr_accessor :callback_url
|
8
|
+
|
9
|
+
VALID_DELIVERY_METHODS = [:polling, :webhook]
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@delivery_method = :polling
|
13
|
+
end
|
14
|
+
|
15
|
+
def delivery_method= value
|
16
|
+
if VALID_DELIVERY_METHODS.include?(value)
|
17
|
+
@delivery_method = value
|
18
|
+
else
|
19
|
+
raise ArgumentError, "No valid reception method. Options available: #{VALID_DELIVERY_METHODS}"
|
20
|
+
end
|
21
|
+
@delivery_method
|
22
|
+
end
|
23
|
+
|
24
|
+
def callback_url= value
|
25
|
+
unless (value =~ URI::regexp).nil?
|
26
|
+
@callback_url = value
|
27
|
+
else
|
28
|
+
raise ArgumentError, "Invalid callback URL"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "open-uri"
|
3
|
+
|
4
|
+
module FantasticRobot
|
5
|
+
class Connection
|
6
|
+
attr_reader :conn
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
# Create the connection object to make calls to the API
|
10
|
+
@conn = Faraday.new(:url => "https://api.telegram.org/bot#{FantasticRobot.configuration.api_key}/") do |faraday|
|
11
|
+
faraday.request :multipart
|
12
|
+
faraday.request :url_encoded
|
13
|
+
faraday.response :logger # log requests to STDOUT
|
14
|
+
faraday.adapter :net_http # make requests with Net::HTTP
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Function to call API passing a payload
|
19
|
+
def api_call method, payload
|
20
|
+
raise ArgumentError, "API method not specified." if method.blank?
|
21
|
+
|
22
|
+
payload ||= {}
|
23
|
+
|
24
|
+
res = @conn.post method.to_s, payload
|
25
|
+
|
26
|
+
raise Faraday::Error, "Wrong response: #{res.inspect}" if (res.status != 200)
|
27
|
+
|
28
|
+
return res
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_by_path path
|
32
|
+
return open("https://api.telegram.org/bot#{FantasticRobot.configuration.api_key}/#{path}")
|
33
|
+
raise Faraday::Error, "Wrong response: #{res.inspect}" if (res.status != 200)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents an audio file to be treated as music by the Telegram clients.
|
3
|
+
class Model::Attachment::Audio < Model::Attachment::Base
|
4
|
+
|
5
|
+
attr_accessor :file_id, :file_size, :duration, :performer, :title, :mime_type
|
6
|
+
|
7
|
+
validates :file_id, :duration, presence: true
|
8
|
+
validates :duration, numericality: true
|
9
|
+
validates :file_size, numericality: true, unless: 'file_size.blank?'
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
module Model
|
3
|
+
module Attachment
|
4
|
+
class Model::Attachment::Base < Model::Base
|
5
|
+
# Function to get the file attached (if there is any)
|
6
|
+
def get_file
|
7
|
+
unless (self.file_id.blank?)
|
8
|
+
request = FantasticRobot::Request::GetFile(file_id: self.file_id)
|
9
|
+
response = FantasticRobot::Model::File.new(JSON.parse(request.api_call))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a phone contact.
|
3
|
+
class Model::Attachment::Contact < Model::Attachment::Base
|
4
|
+
|
5
|
+
attr_accessor :phone_number, :first_name, :last_name, :user_id
|
6
|
+
|
7
|
+
validates :phone_number, :first_name, presence: true
|
8
|
+
validates :user_id, numericality: true, unless: 'user_id.blank?'
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a general file (as opposed to photos, voice messages and audio files).
|
3
|
+
class Model::Attachment::Document < Model::Attachment::Base
|
4
|
+
|
5
|
+
attr_accessor :file_id, :file_size, :thumb, :file_name, :mime_type
|
6
|
+
|
7
|
+
validates :file_id, presence: true
|
8
|
+
validates :file_size, numericality: true, unless: 'file_size.blank?'
|
9
|
+
|
10
|
+
# Field conversions of this model
|
11
|
+
FIELD_CONVERSIONS = {
|
12
|
+
thumb: FantasticRobot::Model::Attachment::PhotoSize
|
13
|
+
}
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a point on the map.
|
3
|
+
class Model::Attachment::Location < Model::Attachment::Base
|
4
|
+
attr_accessor :longitude, :latitude
|
5
|
+
|
6
|
+
validates :longitude, :latitude, presence: true
|
7
|
+
validates :longitude, :latitude, numericality: true
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents one size of a photo or a file / sticker thumbnail.
|
3
|
+
class Model::Attachment::PhotoSize < Model::Attachment::Base
|
4
|
+
|
5
|
+
attr_accessor :file_id, :file_size, :width, :height
|
6
|
+
|
7
|
+
validates :file_id, :width, :height, presence: true
|
8
|
+
validates :width, :height, numericality: true
|
9
|
+
validates :file_size, numericality: true, unless: 'file_size.blank?'
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sticker.
|
3
|
+
class Model::Attachment::Sticker < Model::Attachment::Base
|
4
|
+
|
5
|
+
attr_accessor :file_id, :file_size, :width, :height, :thumb
|
6
|
+
|
7
|
+
validates :file_id, :width, :height, presence: true
|
8
|
+
validates :width, :height, numericality: true
|
9
|
+
validates :file_size, numericality: true, unless: 'file_size.blank?'
|
10
|
+
|
11
|
+
# Field conversions of this model
|
12
|
+
FIELD_CONVERSIONS = {
|
13
|
+
thumb: FantasticRobot::Model::Attachment::PhotoSize
|
14
|
+
}
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a video file.
|
3
|
+
class Model::Attachment::Video < Model::Attachment::Base
|
4
|
+
|
5
|
+
attr_accessor :file_id, :file_size, :width, :height, :duration, :thumb, :mime_type
|
6
|
+
|
7
|
+
validates :file_id, :width, :height, :duration, presence: true
|
8
|
+
validates :width, :height, :duration, numericality: true
|
9
|
+
validates :file_size, numericality: true, unless: 'file_size.blank?'
|
10
|
+
|
11
|
+
# Field conversions of this model
|
12
|
+
FIELD_CONVERSIONS = {
|
13
|
+
thumb: FantasticRobot::Model::Attachment::PhotoSize
|
14
|
+
}
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|