Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
atipugin committed Jun 27, 2015
0 parents commit d6f6235
Show file tree
Hide file tree
Showing 26 changed files with 327 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Gemfile.lock

.bundle/
vendor/bundle/
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
10 changes: 10 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-06-27 20:51:46 +0300 using RuboCop version 0.32.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 15
Style/Documentation:
Enabled: false
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# telegram-bot

Ruby wrapper for [Telegram's Bot API](https://core.telegram.org/bots/api).
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'bundler/gem_tasks'
require 'bundler/setup'
require 'rubocop/rake_task'

RuboCop::RakeTask.new(:rubocop) do |task|
task.fail_on_error = false
task.options = %w(--force-exclusion)
task.patterns = %w(lib/**/*.rb)
end

task default: :rubocop
6 changes: 6 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
require 'bundler/setup'
require 'telegram/bot'

require 'pry'
Pry.start
5 changes: 5 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install
8 changes: 8 additions & 0 deletions lib/telegram/bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'httparty'
require 'persistent_httparty'
require 'virtus'

require 'telegram/bot/api'
require 'telegram/bot/types'
require 'telegram/bot/runner'
require 'telegram/bot/version'
30 changes: 30 additions & 0 deletions lib/telegram/bot/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Telegram
module Bot
class Api
include HTTParty

ENDPOINTS = %w(
getMe sendMessage forwardMessage sendPhoto sendAudio sendDocument
sendSticker sendVideo sendLocation sendChatAction getUserProfilePhotos
getUpdates setWebhook
).freeze

attr_reader :token

base_uri 'https://api.telegram.org'
persistent_connection_adapter

def initialize(token)
@token = token
end

def method_missing(method_name, *args, &block)
ENDPOINTS.include?(method_name.to_s) ? call(method_name, *args) : super
end

def call(endpoint, params = {})
self.class.get("/bot#{token}/#{endpoint}", query: params).to_h
end
end
end
end
33 changes: 33 additions & 0 deletions lib/telegram/bot/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Telegram
module Bot
class Runner
attr_reader :api, :offset

def self.run(*args, &block)
new(*args).run(&block)
end

def initialize(token)
@api = Api.new(token)
@offset = 0
end

def run
yield self
end

def listen
loop do
response = api.getUpdates(offset: offset)
next unless response['ok']

response['result'].each do |data|
update = Types::Update.new(data)
@offset = update.update_id.next
yield update.message
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'telegram/bot/types/base'
require 'telegram/bot/types/user'
require 'telegram/bot/types/group_chat'
require 'telegram/bot/types/audio'
require 'telegram/bot/types/photo_size'
require 'telegram/bot/types/document'
require 'telegram/bot/types/sticker'
require 'telegram/bot/types/video'
require 'telegram/bot/types/contact'
require 'telegram/bot/types/location'
require 'telegram/bot/types/message'
require 'telegram/bot/types/update'
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/audio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Telegram
module Bot
module Types
class Audio < Base
attribute :file_id, String
attribute :duration, Integer
attribute :mime_type, String
attribute :file_size, Integer
end
end
end
end
9 changes: 9 additions & 0 deletions lib/telegram/bot/types/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Telegram
module Bot
module Types
class Base
include Virtus.model
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Telegram
module Bot
module Types
class Contact < Base
attribute :phone_number, String
attribute :first_name, String
attribute :last_name, String
attribute :user_id, String
end
end
end
end
13 changes: 13 additions & 0 deletions lib/telegram/bot/types/document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Telegram
module Bot
module Types
class Document < Base
attribute :file_id, String
attribute :thumb, PhotoSize
attribute :file_name, String
attribute :mime_type, String
attribute :file_size, Integer
end
end
end
end
10 changes: 10 additions & 0 deletions lib/telegram/bot/types/group_chat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Telegram
module Bot
module Types
class GroupChat < Base
attribute :id, Integer
attribute :title, String
end
end
end
end
10 changes: 10 additions & 0 deletions lib/telegram/bot/types/location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Telegram
module Bot
module Types
class Location < Base
attribute :longitude, Float
attribute :latitude, Float
end
end
end
end
39 changes: 39 additions & 0 deletions lib/telegram/bot/types/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Telegram
module Bot
module Types
class Message < Base
attr_accessor :chat

attribute :message_id, Integer
attribute :from, User
attribute :date, Integer
attribute :forward_from, User
attribute :forward_date, Integer
attribute :reply_to_message, Message
attribute :text, String
attribute :audio, Audio
attribute :document, Document
attribute :photo, Array[PhotoSize]
attribute :sticker, Sticker
attribute :video, Video
attribute :contact, Contact
attribute :location, Location
attribute :new_chat_participant, User
attribute :left_chat_participant, User
attribute :new_chat_title, String
attribute :new_chat_photo, Array[PhotoSize]
attribute :delete_chat_photo, Boolean
attribute :group_chat_created, Boolean

def chat=(value)
@chat =
if value.key?('username')
User.new(value)
elsif value.key?('title')
GroupChat.new(value)
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/photo_size.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Telegram
module Bot
module Types
class PhotoSize < Base
attribute :file_id, String
attribute :width, Integer
attribute :height, Integer
attribute :file_size, Integer
end
end
end
end
13 changes: 13 additions & 0 deletions lib/telegram/bot/types/sticker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Telegram
module Bot
module Types
class Sticker < Base
attribute :file_id, String
attribute :width, Integer
attribute :height, Integer
attribute :thumb, PhotoSize
attribute :file_size, Integer
end
end
end
end
10 changes: 10 additions & 0 deletions lib/telegram/bot/types/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Telegram
module Bot
module Types
class Update < Base
attribute :update_id, Integer
attribute :message, Message
end
end
end
end
12 changes: 12 additions & 0 deletions lib/telegram/bot/types/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Telegram
module Bot
module Types
class User < Base
attribute :id, Integer
attribute :first_name, String
attribute :last_name, String
attribute :username, String
end
end
end
end
16 changes: 16 additions & 0 deletions lib/telegram/bot/types/video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Telegram
module Bot
module Types
class Video < Base
attribute :file_id, String
attribute :width, Integer
attribute :height, Integer
attribute :duration, Integer
attribute :thumb, PhotoSize
attribute :mime_type, String
attribute :file_size, Integer
attribute :caption, String
end
end
end
end
5 changes: 5 additions & 0 deletions lib/telegram/bot/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Telegram
module Bot
VERSION = '0.1.0'
end
end
28 changes: 28 additions & 0 deletions telegram-bot.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'telegram/bot/version'

Gem::Specification.new do |spec|
spec.name = 'telegram-bot'
spec.version = Telegram::Bot::VERSION
spec.authors = ['Alexander Tipugin']
spec.email = ['[email protected]']

spec.summary = "Ruby wrapper for Telegram's Bot API"
spec.homepage = 'https://github.com/atipugin/telegram-bot'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'httparty'
spec.add_dependency 'persistent_httparty'
spec.add_dependency 'virtus'

spec.add_development_dependency 'bundler', '~> 1.9'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'rubocop'
end

0 comments on commit d6f6235

Please sign in to comment.