telegraphist 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5fa7605a06dd8e4c94186d70a14df21e98070d3c
4
+ data.tar.gz: bdb93a9f4611abac5780ba3c9f3d56e438faefbd
5
+ SHA512:
6
+ metadata.gz: b07e14b980559c89c6fd69e7e93259ef90ec7abed4035256a8b24f5cc8f3aff516c9ba1f418297c0a1dd3ef9e9e35338fb70ee571ef9aea2390cd61138e841e3
7
+ data.tar.gz: e2e362123736f920eabe596dc51988711690dd65d7fceb0e94343d954bd6b278c285e267d81d57b98b12913f303bb3b79869cd845a176cec46ffe62335d6594a
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ /.idea
12
+ /spec/test_api_key.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -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, 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/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem 'httparty'
5
+ gem 'activemodel', '~> 4.0'
6
+
7
+ group :test do
8
+ gem 'rspec', '~> 3.3.0'
9
+ gem 'vcr', '~> 2.9.3'
10
+ gem 'webmock'
11
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 dizer
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.
@@ -0,0 +1,44 @@
1
+ # Telegraphist
2
+
3
+ Telegram Bot API gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'telegraphist'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install telegraphist
20
+
21
+ ## Usage
22
+
23
+ Echo bot:
24
+
25
+ ```ruby
26
+ bot = Telegraphist::Bot.create('NNNNNNNN:SSSSSSSSSSSSSSSSSSSSSSS')
27
+ bot.update_loop do |update|
28
+ bot.send_message(update.message.chat.id, update.message.text)
29
+ end
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( https://github.com/dizer/telegraphist/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "telegraphist"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,14 @@
1
+ require 'active_model'
2
+ require 'httparty'
3
+
4
+ require 'telegraphist/version'
5
+ require 'telegraphist/bot'
6
+ require 'telegraphist/client'
7
+ require 'telegraphist/model'
8
+ require 'telegraphist/model/group_chat'
9
+ require 'telegraphist/model/message'
10
+ require 'telegraphist/model/update'
11
+ require 'telegraphist/model/user'
12
+
13
+ module Telegraphist
14
+ end
@@ -0,0 +1,33 @@
1
+ module Telegraphist
2
+ class Bot
3
+ include ActiveModel::Model
4
+
5
+ attr_accessor :id, :first_name, :username, :client
6
+
7
+ def self.create(authentication_token)
8
+ client = Client.new(authentication_token)
9
+ new(client.get(:getMe)['result'].merge(client: client))
10
+ end
11
+
12
+ def update_loop(&block)
13
+ last_update = nil
14
+ loop do
15
+ updates(last_update).try(:each) do |update|
16
+ block.call(update)
17
+ last_update = [last_update, update.update_id + 1].compact.max
18
+ end
19
+ sleep 1
20
+ end
21
+ end
22
+
23
+ def updates(offset=nil)
24
+ (client.get(:getUpdates, query: {offset: offset})['result'] || []).map do |update|
25
+ Model::Update.new(update)
26
+ end
27
+ end
28
+
29
+ def send_message(chat_id, text)
30
+ client.post(:sendMessage, query: {chat_id: chat_id, text: text})
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ module Telegraphist
2
+ class Client
3
+ include HTTParty
4
+ base_uri 'https://api.telegram.org/'
5
+
6
+ attr_reader :options, :authentication_token
7
+
8
+ def initialize(authentication_token)
9
+ @authentication_token = authentication_token
10
+ @options = {}
11
+ end
12
+
13
+ def get(path, payload={})
14
+ self.class.get(url(path), options.deep_merge(payload)).parsed_response
15
+ end
16
+
17
+ def post(path, payload={})
18
+ self.class.post(url(path), options.deep_merge(payload)).parsed_response
19
+ end
20
+
21
+ def url(path)
22
+ ['/', 'bot', authentication_token, '/', path].join
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module Telegraphist
3
+ class Model
4
+ include ::ActiveModel::Model
5
+ end
6
+ end
7
+
@@ -0,0 +1,5 @@
1
+ module Telegraphist
2
+ class Model::GroupChat < Model
3
+ attr_accessor :id, :title
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ module Telegraphist
2
+ class Model::Message < Model
3
+ attr_accessor :message_id, :from, :date, :chat, :forward_from, :forward_date, :reply_to_message, :text, :audio,
4
+ :document, :photo, :sticker, :video, :contact, :location, :new_chat_participant,
5
+ :left_chat_participant, :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created
6
+
7
+ def from=(options={})
8
+ @from = User.new(options)
9
+ end
10
+
11
+ def chat=(options={})
12
+ @chat = if options['title']
13
+ GroupChat.new(options)
14
+ else
15
+ User.new(options)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module Telegraphist
2
+ class Model::Update < Model
3
+ attr_accessor :update_id, :message
4
+
5
+ def message=(options={})
6
+ @message = Message.new(options)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Telegraphist
2
+ class Model::User < Model
3
+ attr_accessor :id, :first_name, :last_name, :username
4
+
5
+ def to_s
6
+ [id, username, [first_name, last_name].compact.join(' ')].join(' :: ')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Telegraphist
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'telegraphist/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "telegraphist"
8
+ spec.version = Telegraphist::VERSION
9
+ spec.authors = ["Yury Alexandrov"]
10
+ spec.email = ["dizeru@gmail.com"]
11
+
12
+ spec.summary = %q{Telegram Bot API client}
13
+ spec.homepage = "https://github.com/dizer/telegraphist"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegraphist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yury Alexandrov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - dizeru@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/telegraphist.rb
59
+ - lib/telegraphist/bot.rb
60
+ - lib/telegraphist/client.rb
61
+ - lib/telegraphist/model.rb
62
+ - lib/telegraphist/model/group_chat.rb
63
+ - lib/telegraphist/model/message.rb
64
+ - lib/telegraphist/model/update.rb
65
+ - lib/telegraphist/model/user.rb
66
+ - lib/telegraphist/version.rb
67
+ - telegraphist.gemspec
68
+ homepage: https://github.com/dizer/telegraphist
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.6
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Telegram Bot API client
92
+ test_files: []