telegram-bot-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6275009e6d9d48cb1baa1eb7f4708ca5e15367c4
4
+ data.tar.gz: 8068393666fa87920092009459eaefd08602225d
5
+ SHA512:
6
+ metadata.gz: c32c1cfa04b6b71cba29598c8973c0692ed0448e56896930bd1bb57e9ca5b66b57a524dc3aa40e082524e0db968aa9cf7220f622977b6cb9f1c9ce853c3ee0c6
7
+ data.tar.gz: 1b51d368865b665dda6d057e9f5d675c5a389b5bdb28f8bc0bce070ccf7eba29838829a3559b0f5a5c9e2c1bc0b62eaa8c196e4c081d44a3862f34af14a24baa
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ Gemfile.lock
2
+
3
+ .bundle/
4
+ pkg/
5
+ vendor/bundle/
data/.rubocop.yml ADDED
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,10 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-06-27 20:51:46 +0300 using RuboCop version 0.32.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 15
9
+ Style/Documentation:
10
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # telegram-bot
2
+
3
+ Ruby wrapper for [Telegram's Bot API](https://core.telegram.org/bots/api).
4
+
5
+ ## Installation
6
+
7
+ Add following line to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'telegram-bot', github: 'atipugin/telegram-bot'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```shell
16
+ $ bundle
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ First things first, you need to [obtain a token](https://core.telegram.org/bots#botfather) for your bot. Then create your Telegram bot like this:
22
+
23
+ ```ruby
24
+ token = 'YOUR_TELEGRAM_BOT_API_TOKEN'
25
+
26
+ Telegram::Bot::Runner.run(token) do |bot|
27
+ bot.listen do |message|
28
+ case message.text
29
+ when /^hello/
30
+ bot.api.sendMessage(chat_id: message.chat.id, text: "Hello, #{message.from.username}")
31
+ end
32
+ end
33
+ end
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (git checkout -b my-new-feature)
40
+ 3. Commit your changes (git commit -am 'Add some feature')
41
+ 4. Push to the branch (git push origin my-new-feature)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'bundler/setup'
3
+ require 'rubocop/rake_task'
4
+
5
+ RuboCop::RakeTask.new(:rubocop) do |task|
6
+ task.fail_on_error = false
7
+ task.options = %w(--force-exclusion)
8
+ task.patterns = %w(lib/**/*.rb)
9
+ end
10
+
11
+ task default: :rubocop
data/bin/console ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'telegram/bot'
4
+
5
+ require 'pry'
6
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,8 @@
1
+ require 'httparty'
2
+ require 'persistent_httparty'
3
+ require 'virtus'
4
+
5
+ require 'telegram/bot/api'
6
+ require 'telegram/bot/types'
7
+ require 'telegram/bot/runner'
8
+ require 'telegram/bot/version'
@@ -0,0 +1,30 @@
1
+ module Telegram
2
+ module Bot
3
+ class Api
4
+ include HTTParty
5
+
6
+ ENDPOINTS = %w(
7
+ getMe sendMessage forwardMessage sendPhoto sendAudio sendDocument
8
+ sendSticker sendVideo sendLocation sendChatAction getUserProfilePhotos
9
+ getUpdates setWebhook
10
+ ).freeze
11
+
12
+ attr_reader :token
13
+
14
+ base_uri 'https://api.telegram.org'
15
+ persistent_connection_adapter
16
+
17
+ def initialize(token)
18
+ @token = token
19
+ end
20
+
21
+ def method_missing(method_name, *args, &block)
22
+ ENDPOINTS.include?(method_name.to_s) ? call(method_name, *args) : super
23
+ end
24
+
25
+ def call(endpoint, params = {})
26
+ self.class.get("/bot#{token}/#{endpoint}", query: params).to_h
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ module Telegram
2
+ module Bot
3
+ class Runner
4
+ attr_reader :api, :offset
5
+
6
+ def self.run(*args, &block)
7
+ new(*args).run(&block)
8
+ end
9
+
10
+ def initialize(token)
11
+ @api = Api.new(token)
12
+ @offset = 0
13
+ end
14
+
15
+ def run
16
+ yield self
17
+ end
18
+
19
+ def listen
20
+ loop do
21
+ response = api.getUpdates(offset: offset)
22
+ next unless response['ok']
23
+
24
+ response['result'].each do |data|
25
+ update = Types::Update.new(data)
26
+ @offset = update.update_id.next
27
+ yield update.message
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ require 'telegram/bot/types/base'
2
+ require 'telegram/bot/types/user'
3
+ require 'telegram/bot/types/group_chat'
4
+ require 'telegram/bot/types/audio'
5
+ require 'telegram/bot/types/photo_size'
6
+ require 'telegram/bot/types/document'
7
+ require 'telegram/bot/types/sticker'
8
+ require 'telegram/bot/types/video'
9
+ require 'telegram/bot/types/contact'
10
+ require 'telegram/bot/types/location'
11
+ require 'telegram/bot/types/message'
12
+ require 'telegram/bot/types/update'
@@ -0,0 +1,12 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Audio < Base
5
+ attribute :file_id, String
6
+ attribute :duration, Integer
7
+ attribute :mime_type, String
8
+ attribute :file_size, Integer
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Base
5
+ include Virtus.model
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Contact < Base
5
+ attribute :phone_number, String
6
+ attribute :first_name, String
7
+ attribute :last_name, String
8
+ attribute :user_id, String
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Document < Base
5
+ attribute :file_id, String
6
+ attribute :thumb, PhotoSize
7
+ attribute :file_name, String
8
+ attribute :mime_type, String
9
+ attribute :file_size, Integer
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class GroupChat < Base
5
+ attribute :id, Integer
6
+ attribute :title, String
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Location < Base
5
+ attribute :longitude, Float
6
+ attribute :latitude, Float
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Message < Base
5
+ attr_accessor :chat
6
+
7
+ attribute :message_id, Integer
8
+ attribute :from, User
9
+ attribute :date, Integer
10
+ attribute :forward_from, User
11
+ attribute :forward_date, Integer
12
+ attribute :reply_to_message, Message
13
+ attribute :text, String
14
+ attribute :audio, Audio
15
+ attribute :document, Document
16
+ attribute :photo, Array[PhotoSize]
17
+ attribute :sticker, Sticker
18
+ attribute :video, Video
19
+ attribute :contact, Contact
20
+ attribute :location, Location
21
+ attribute :new_chat_participant, User
22
+ attribute :left_chat_participant, User
23
+ attribute :new_chat_title, String
24
+ attribute :new_chat_photo, Array[PhotoSize]
25
+ attribute :delete_chat_photo, Boolean
26
+ attribute :group_chat_created, Boolean
27
+
28
+ def chat=(value)
29
+ @chat =
30
+ if value.key?('username')
31
+ User.new(value)
32
+ elsif value.key?('title')
33
+ GroupChat.new(value)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class PhotoSize < Base
5
+ attribute :file_id, String
6
+ attribute :width, Integer
7
+ attribute :height, Integer
8
+ attribute :file_size, Integer
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Sticker < Base
5
+ attribute :file_id, String
6
+ attribute :width, Integer
7
+ attribute :height, Integer
8
+ attribute :thumb, PhotoSize
9
+ attribute :file_size, Integer
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Update < Base
5
+ attribute :update_id, Integer
6
+ attribute :message, Message
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class User < Base
5
+ attribute :id, Integer
6
+ attribute :first_name, String
7
+ attribute :last_name, String
8
+ attribute :username, String
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class Video < Base
5
+ attribute :file_id, String
6
+ attribute :width, Integer
7
+ attribute :height, Integer
8
+ attribute :duration, Integer
9
+ attribute :thumb, PhotoSize
10
+ attribute :mime_type, String
11
+ attribute :file_size, Integer
12
+ attribute :caption, String
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Telegram
2
+ module Bot
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'telegram/bot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'telegram-bot-ruby'
8
+ spec.version = Telegram::Bot::VERSION
9
+ spec.authors = ['Alexander Tipugin']
10
+ spec.email = ['atipugin@gmail.com']
11
+
12
+ spec.summary = "Ruby wrapper for Telegram's Bot API"
13
+ spec.homepage = 'https://github.com/atipugin/telegram-bot'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'httparty'
21
+ spec.add_dependency 'persistent_httparty'
22
+ spec.add_dependency 'virtus'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.9'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'rubocop'
28
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegram-bot-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Tipugin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: persistent_httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: virtus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - atipugin@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - ".rubocop_todo.yml"
121
+ - Gemfile
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - lib/telegram/bot.rb
127
+ - lib/telegram/bot/api.rb
128
+ - lib/telegram/bot/runner.rb
129
+ - lib/telegram/bot/types.rb
130
+ - lib/telegram/bot/types/audio.rb
131
+ - lib/telegram/bot/types/base.rb
132
+ - lib/telegram/bot/types/contact.rb
133
+ - lib/telegram/bot/types/document.rb
134
+ - lib/telegram/bot/types/group_chat.rb
135
+ - lib/telegram/bot/types/location.rb
136
+ - lib/telegram/bot/types/message.rb
137
+ - lib/telegram/bot/types/photo_size.rb
138
+ - lib/telegram/bot/types/sticker.rb
139
+ - lib/telegram/bot/types/update.rb
140
+ - lib/telegram/bot/types/user.rb
141
+ - lib/telegram/bot/types/video.rb
142
+ - lib/telegram/bot/version.rb
143
+ - telegram-bot.gemspec
144
+ homepage: https://github.com/atipugin/telegram-bot
145
+ licenses: []
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.4.5
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Ruby wrapper for Telegram's Bot API
167
+ test_files: []