chatbot_helper-telegram 0.1.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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +129 -0
  3. data/.travis.yml +14 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +128 -0
  7. data/Rakefile +10 -0
  8. data/bin/console +14 -0
  9. data/bin/setup +8 -0
  10. data/chatbot_helper-telegram.gemspec +43 -0
  11. data/lib/chatbot_helper/exceptions.rb +27 -0
  12. data/lib/chatbot_helper/telegram.rb +47 -0
  13. data/lib/chatbot_helper/telegram/animation.rb +22 -0
  14. data/lib/chatbot_helper/telegram/audio.rb +16 -0
  15. data/lib/chatbot_helper/telegram/base_resource.rb +254 -0
  16. data/lib/chatbot_helper/telegram/callback_game.rb +11 -0
  17. data/lib/chatbot_helper/telegram/callback_query.rb +29 -0
  18. data/lib/chatbot_helper/telegram/chat.rb +16 -0
  19. data/lib/chatbot_helper/telegram/chat_member.rb +18 -0
  20. data/lib/chatbot_helper/telegram/chosen_inline_result.rb +29 -0
  21. data/lib/chatbot_helper/telegram/collection_resource.rb +50 -0
  22. data/lib/chatbot_helper/telegram/contact.rb +16 -0
  23. data/lib/chatbot_helper/telegram/document.rb +22 -0
  24. data/lib/chatbot_helper/telegram/file.rb +16 -0
  25. data/lib/chatbot_helper/telegram/force_reply.rb +16 -0
  26. data/lib/chatbot_helper/telegram/game.rb +35 -0
  27. data/lib/chatbot_helper/telegram/inline_keyboard_button.rb +36 -0
  28. data/lib/chatbot_helper/telegram/inline_keyboard_markup.rb +16 -0
  29. data/lib/chatbot_helper/telegram/inline_query.rb +24 -0
  30. data/lib/chatbot_helper/telegram/keyboard_button.rb +69 -0
  31. data/lib/chatbot_helper/telegram/location.rb +12 -0
  32. data/lib/chatbot_helper/telegram/message.rb +55 -0
  33. data/lib/chatbot_helper/telegram/message_entity.rb +23 -0
  34. data/lib/chatbot_helper/telegram/photo_size.rb +25 -0
  35. data/lib/chatbot_helper/telegram/reply_keyboard_markup.rb +20 -0
  36. data/lib/chatbot_helper/telegram/reply_keyboard_remove.rb +17 -0
  37. data/lib/chatbot_helper/telegram/response_parameters.rb +13 -0
  38. data/lib/chatbot_helper/telegram/sticker.rb +22 -0
  39. data/lib/chatbot_helper/telegram/toolbox.rb +27 -0
  40. data/lib/chatbot_helper/telegram/update.rb +28 -0
  41. data/lib/chatbot_helper/telegram/user.rb +16 -0
  42. data/lib/chatbot_helper/telegram/user_profile_photos.rb +20 -0
  43. data/lib/chatbot_helper/telegram/venue.rb +22 -0
  44. data/lib/chatbot_helper/telegram/version.rb +5 -0
  45. data/lib/chatbot_helper/telegram/video.rb +22 -0
  46. data/lib/chatbot_helper/telegram/voice.rb +16 -0
  47. metadata +176 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b33926914f85db219452360ab585b1273a7d20ae
4
+ data.tar.gz: dc4f9f80efcdb2c28054805e698b97326ba4577e
5
+ SHA512:
6
+ metadata.gz: 9244d79ce09e99036455c4fd380c6ca8869df5876c3c2707ce7df8e18646725909489bcc9f667ec4e7402924c07db3ddb1a49380e482d9f53e61cf129f492317
7
+ data.tar.gz: 663e54f9c9ee8b19d988eeda2053fae94760d5d9f934cd683a3f75463b2846be6ca8cada7895e0277261b0bb12451fda8b5636c98091cba8cdc4a4c899c81d25
data/.gitignore ADDED
@@ -0,0 +1,129 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ # Don't check in Gemfile.lock for gems.
4
+ # Explanation: http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
12
+
13
+ # Created by https://www.gitignore.io/api/macos,windows,rubymine
14
+
15
+ ### macOS ###
16
+ *.DS_Store
17
+ .AppleDouble
18
+ .LSOverride
19
+
20
+ # Icon must end with two \r
21
+ Icon
22
+
23
+ # Thumbnails
24
+ ._*
25
+
26
+ # Files that might appear in the root of a volume
27
+ .DocumentRevisions-V100
28
+ .fseventsd
29
+ .Spotlight-V100
30
+ .TemporaryItems
31
+ .Trashes
32
+ .VolumeIcon.icns
33
+ .com.apple.timemachine.donotpresent
34
+
35
+ # Directories potentially created on remote AFP share
36
+ .AppleDB
37
+ .AppleDesktop
38
+ Network Trash Folder
39
+ Temporary Items
40
+ .apdisk
41
+
42
+ ### RubyMine ###
43
+ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
44
+ # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
45
+
46
+ # User-specific stuff:
47
+ .idea/**/workspace.xml
48
+ .idea/**/tasks.xml
49
+ .idea/dictionaries
50
+
51
+ # Sensitive or high-churn files:
52
+ .idea/**/dataSources/
53
+ .idea/**/dataSources.ids
54
+ .idea/**/dataSources.xml
55
+ .idea/**/dataSources.local.xml
56
+ .idea/**/sqlDataSources.xml
57
+ .idea/**/dynamic.xml
58
+ .idea/**/uiDesigner.xml
59
+
60
+ # Gradle:
61
+ .idea/**/gradle.xml
62
+ .idea/**/libraries
63
+
64
+ # CMake
65
+ cmake-build-debug/
66
+
67
+ # Mongo Explorer plugin:
68
+ .idea/**/mongoSettings.xml
69
+
70
+ ## File-based project format:
71
+ *.iws
72
+
73
+ ## Plugin-specific files:
74
+
75
+ # IntelliJ
76
+ /out/
77
+
78
+ # mpeltonen/sbt-idea plugin
79
+ .idea_modules/
80
+
81
+ # JIRA plugin
82
+ atlassian-ide-plugin.xml
83
+
84
+ # Cursive Clojure plugin
85
+ .idea/replstate.xml
86
+
87
+ # Crashlytics plugin (for Android Studio and IntelliJ)
88
+ com_crashlytics_export_strings.xml
89
+ crashlytics.properties
90
+ crashlytics-build.properties
91
+ fabric.properties
92
+
93
+ ### RubyMine Patch ###
94
+ # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
95
+
96
+ # *.iml
97
+ # modules.xml
98
+ # .idea/misc.xml
99
+ # *.ipr
100
+
101
+ # Sonarlint plugin
102
+ .idea/sonarlint
103
+
104
+ ### Windows ###
105
+ # Windows thumbnail cache files
106
+ Thumbs.db
107
+ ehthumbs.db
108
+ ehthumbs_vista.db
109
+
110
+ # Folder config file
111
+ Desktop.ini
112
+
113
+ # Recycle Bin used on file shares
114
+ $RECYCLE.BIN/
115
+
116
+ # Windows Installer files
117
+ *.cab
118
+ *.msi
119
+ *.msm
120
+ *.msp
121
+
122
+ # Windows shortcuts
123
+ *.lnk
124
+
125
+ # End of https://www.gitignore.io/api/macos,windows,rubymine
126
+
127
+ # Don't check in the .idea directory
128
+ # This can be discussed but for now it doesn't make any sense to check it in
129
+ .idea/
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.10
5
+ - 2.2.7
6
+ - 2.3.4
7
+ - 2.4.1
8
+ before_install: gem install bundler -v 1.14.6
9
+ # code climate plugin
10
+ addons:
11
+ code_climate:
12
+ repo_token: 9e0693f87fb7a67ceb8b36f123dc70f6dd8a752d61faf47cfedad87932242193
13
+ after_success:
14
+ - bundle exec codeclimate-test-reporter
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chatbot_helper-telegram.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ybrin
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # ChatbotHelper::Telegram [![Build Status](https://travis-ci.org/Ybrin/chatbot_helper-telegram.svg?branch=master)](https://travis-ci.org/Ybrin/chatbot_helper-telegram) [![Code Climate](https://codeclimate.com/github/Ybrin/chatbot_helper-telegram/badges/gpa.svg)](https://codeclimate.com/github/Ybrin/chatbot_helper-telegram) [![Test Coverage](https://codeclimate.com/github/Ybrin/chatbot_helper-telegram/badges/coverage.svg)](https://codeclimate.com/github/Ybrin/chatbot_helper-telegram/coverage)
2
+
3
+ `ChatbotHelper::Telegram` helps you build awesome Telegram chatbots without bothering much about the documentation, the message structure or other details. It is a wrapper around the [Telegram Bot API](https://core.telegram.org/bots/api).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'chatbot_helper-telegram'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install chatbot_helper-telegram
20
+
21
+ Then require it and you are good to go:
22
+
23
+ ```ruby
24
+ require 'chatbot_helper/telegram'
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ As of now, you are responsible for getting messages or other objects from the Telegram Bot API with your favorite HTTP library. We consider adding all Bot API methods to this library in one of the next versions. Please open an [issue](https://github.com/Ybrin/chatbot_helper-telegram/issues) if you think that's a good idea.
30
+
31
+ An example with [typhoeus](https://github.com/typhoeus/typhoeus):
32
+
33
+ ```ruby
34
+ require 'typhoeus'
35
+ require 'chatbot_helper/telegram'
36
+ require 'json'
37
+
38
+ # Your environment must have set the `TELEGRAM_TOKEN` key to your telegram api
39
+ # token. See https://core.telegram.org/bots#3-how-do-i-create-a-bot for more
40
+ # information.
41
+ telegram_token = ENV['TELEGRAM_TOKEN']
42
+ url = "https://api.telegram.org/bot#{telegram_token}/sendMessage"
43
+
44
+ # We create a message
45
+ message = {
46
+ chat_id: 123456,
47
+ text: "This is an example message sent by my telegram bot"
48
+ }
49
+
50
+ request = Typhoeus::Request.new(
51
+ url,
52
+ method: :post,
53
+ body: JSON.generate(message),
54
+ headers: { 'Content-Type' => 'application/json' }
55
+ )
56
+ request.run
57
+
58
+ body = request.response.body
59
+
60
+ # We create a Message object from the response body
61
+ message = ChatbotHelper::Telegram::Message.new(string: body)
62
+
63
+ # Now you can access all properties of the message object
64
+ puts message.date
65
+ puts message.from.first_name
66
+
67
+ # Yay!
68
+ ```
69
+
70
+ The real power of this library is the update and command parsing.
71
+ Say, for example, you have set up [webhooks](https://core.telegram.org/bots/api#setwebhook) and you want to parse the updates retrieved from these webhook requests. This is a [sinatra](http://www.sinatrarb.com/) example but you can easily convert it to Rails code.
72
+
73
+ ```ruby
74
+ require 'sinatra'
75
+ require 'chatbot_helper/telegram'
76
+ require 'json'
77
+
78
+ post "/#{ENV['TELEGRAM_TOKEN']}/?" do
79
+ request.body.rewind
80
+ payload_body = request.body.read
81
+
82
+ # Create an instance of Update from the request body...
83
+ update = ChatbotHelper::Telegram::Update.new(string: payload_body)
84
+
85
+ # Return a message based on the given command
86
+ message = {}
87
+ if update.message
88
+ # Access the chat id through the given message
89
+ message[:chat_id] = update.message.chat.id
90
+
91
+ case update.message.text
92
+ when '/start'
93
+ text = "Hello and welcome to my awesome bot #{update.chat.first_name}!"
94
+ message[:text] = text
95
+ when '/saysomething'
96
+ text = "Why should I talk with you, #{update.chat.first_name}? Give me "\
97
+ "a reason!"
98
+ message[:text] = text
99
+ end
100
+
101
+ # Return the message which should be sent
102
+ return halt 200, { 'Content-Type' => 'application/json' }, JSON.generate(message)
103
+ end
104
+
105
+ # Fallback, return nothing
106
+ return halt 200, { 'Content-Type' => 'application/json' }, '{}'
107
+ end
108
+ ```
109
+
110
+ You can work with nearly all Telegram Bot API [available types](https://core.telegram.org/bots/api#available-types).
111
+ One exception are the `Inline mode` types which has currently just partial support and will be fully available in the next version.
112
+
113
+ If you need help or have a feature request feel free and open an [issue](https://github.com/Ybrin/chatbot_helper-telegram/issues).
114
+
115
+ ## Development
116
+
117
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
118
+
119
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
120
+
121
+ ## Contributing
122
+
123
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Ybrin/chatbot_helper-telegram.
124
+
125
+
126
+ ## License
127
+
128
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chatbot_helper/telegram"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'chatbot_helper/telegram/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'chatbot_helper-telegram'
9
+ spec.version = ChatbotHelper::Telegram::VERSION
10
+ spec.authors = ['Koray Koska']
11
+ spec.email = ['koray@koska.at']
12
+
13
+ spec.summary = %q{Easily handle Telegram chatbot messages in Ruby}
14
+ spec.description = %q{This library tries to help working with messages from the Telegram chatbot API. Messages are parsed and useful methods provide all needed information about the message.}
15
+ spec.homepage = 'https://github.com/Ybrin/chatbot_helper-telegram'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ # Development dependencies
35
+ spec.add_development_dependency 'bundler', '~> 1.13'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'minitest', '~> 5.0'
38
+ spec.add_development_dependency 'simplecov'
39
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
40
+
41
+ # Project dependencies
42
+ spec.add_dependency 'json', '~> 2.0'
43
+ end
@@ -0,0 +1,27 @@
1
+ module ChatbotHelper
2
+ module Exceptions
3
+ # A default Error class which should not be used but instead subclassed
4
+ class Error < RuntimeError
5
+ attr_reader :errors
6
+
7
+ def initialize(*errors)
8
+ @errors = errors
9
+ end
10
+
11
+ class << self
12
+ def description
13
+ raise NotImplementedError, 'Description of subclass not implemented'
14
+ end
15
+ end
16
+ end
17
+
18
+ # An invalid resource error
19
+ class InvalidResource < Error
20
+ class << self
21
+ def description
22
+ 'A resource is invalid or does not follow its specification'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,47 @@
1
+ require 'chatbot_helper/telegram/version'
2
+
3
+ # BaseResource
4
+ require 'chatbot_helper/telegram/base_resource'
5
+ # Resources
6
+ require 'chatbot_helper/telegram/animation'
7
+ require 'chatbot_helper/telegram/audio'
8
+ require 'chatbot_helper/telegram/callback_game'
9
+ require 'chatbot_helper/telegram/callback_query'
10
+ require 'chatbot_helper/telegram/chat'
11
+ require 'chatbot_helper/telegram/chat_member'
12
+ require 'chatbot_helper/telegram/chosen_inline_result'
13
+ require 'chatbot_helper/telegram/collection_resource'
14
+ require 'chatbot_helper/telegram/contact'
15
+ require 'chatbot_helper/telegram/document'
16
+ require 'chatbot_helper/telegram/file'
17
+ require 'chatbot_helper/telegram/force_reply'
18
+ require 'chatbot_helper/telegram/game'
19
+ require 'chatbot_helper/telegram/inline_keyboard_button'
20
+ require 'chatbot_helper/telegram/inline_keyboard_markup'
21
+ require 'chatbot_helper/telegram/inline_query'
22
+ require 'chatbot_helper/telegram/keyboard_button'
23
+ require 'chatbot_helper/telegram/location'
24
+ require 'chatbot_helper/telegram/message'
25
+ require 'chatbot_helper/telegram/message_entity'
26
+ require 'chatbot_helper/telegram/photo_size'
27
+ require 'chatbot_helper/telegram/reply_keyboard_markup'
28
+ require 'chatbot_helper/telegram/reply_keyboard_remove'
29
+ require 'chatbot_helper/telegram/response_parameters'
30
+ require 'chatbot_helper/telegram/sticker'
31
+ require 'chatbot_helper/telegram/update'
32
+ require 'chatbot_helper/telegram/user'
33
+ require 'chatbot_helper/telegram/user_profile_photos'
34
+ require 'chatbot_helper/telegram/venue'
35
+ require 'chatbot_helper/telegram/video'
36
+ require 'chatbot_helper/telegram/voice'
37
+
38
+ require 'chatbot_helper/telegram/toolbox'
39
+
40
+ # Exceptions
41
+ require 'chatbot_helper/exceptions'
42
+
43
+ module ChatbotHelper
44
+ module Telegram
45
+ # Your code goes here...
46
+ end
47
+ end