From d6f6235532912b64979206c63d0e687d97513a5d Mon Sep 17 00:00:00 2001 From: Alexander Tipugin Date: Sat, 27 Jun 2015 20:53:40 +0300 Subject: [PATCH] First commit --- .gitignore | 4 +++ .rubocop.yml | 1 + .rubocop_todo.yml | 10 +++++++ Gemfile | 3 +++ README.md | 3 +++ Rakefile | 11 ++++++++ bin/console | 6 +++++ bin/setup | 5 ++++ lib/telegram/bot.rb | 8 ++++++ lib/telegram/bot/api.rb | 30 +++++++++++++++++++++ lib/telegram/bot/runner.rb | 33 +++++++++++++++++++++++ lib/telegram/bot/types.rb | 12 +++++++++ lib/telegram/bot/types/audio.rb | 12 +++++++++ lib/telegram/bot/types/base.rb | 9 +++++++ lib/telegram/bot/types/contact.rb | 12 +++++++++ lib/telegram/bot/types/document.rb | 13 ++++++++++ lib/telegram/bot/types/group_chat.rb | 10 +++++++ lib/telegram/bot/types/location.rb | 10 +++++++ lib/telegram/bot/types/message.rb | 39 ++++++++++++++++++++++++++++ lib/telegram/bot/types/photo_size.rb | 12 +++++++++ lib/telegram/bot/types/sticker.rb | 13 ++++++++++ lib/telegram/bot/types/update.rb | 10 +++++++ lib/telegram/bot/types/user.rb | 12 +++++++++ lib/telegram/bot/types/video.rb | 16 ++++++++++++ lib/telegram/bot/version.rb | 5 ++++ telegram-bot.gemspec | 28 ++++++++++++++++++++ 26 files changed, 327 insertions(+) create mode 100644 .gitignore create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml create mode 100644 Gemfile create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/console create mode 100755 bin/setup create mode 100644 lib/telegram/bot.rb create mode 100644 lib/telegram/bot/api.rb create mode 100644 lib/telegram/bot/runner.rb create mode 100644 lib/telegram/bot/types.rb create mode 100644 lib/telegram/bot/types/audio.rb create mode 100644 lib/telegram/bot/types/base.rb create mode 100644 lib/telegram/bot/types/contact.rb create mode 100644 lib/telegram/bot/types/document.rb create mode 100644 lib/telegram/bot/types/group_chat.rb create mode 100644 lib/telegram/bot/types/location.rb create mode 100644 lib/telegram/bot/types/message.rb create mode 100644 lib/telegram/bot/types/photo_size.rb create mode 100644 lib/telegram/bot/types/sticker.rb create mode 100644 lib/telegram/bot/types/update.rb create mode 100644 lib/telegram/bot/types/user.rb create mode 100644 lib/telegram/bot/types/video.rb create mode 100644 lib/telegram/bot/version.rb create mode 100644 telegram-bot.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab69b63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +Gemfile.lock + +.bundle/ +vendor/bundle/ diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..cc32da4 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1 @@ +inherit_from: .rubocop_todo.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..430fab6 --- /dev/null +++ b/.rubocop_todo.yml @@ -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 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/README.md b/README.md new file mode 100644 index 0000000..bcbc28e --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# telegram-bot + +Ruby wrapper for [Telegram's Bot API](https://core.telegram.org/bots/api). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..aed873e --- /dev/null +++ b/Rakefile @@ -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 diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..ae40b0f --- /dev/null +++ b/bin/console @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby +require 'bundler/setup' +require 'telegram/bot' + +require 'pry' +Pry.start diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..7913e05 --- /dev/null +++ b/bin/setup @@ -0,0 +1,5 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +bundle install diff --git a/lib/telegram/bot.rb b/lib/telegram/bot.rb new file mode 100644 index 0000000..bdb0164 --- /dev/null +++ b/lib/telegram/bot.rb @@ -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' diff --git a/lib/telegram/bot/api.rb b/lib/telegram/bot/api.rb new file mode 100644 index 0000000..9f91a59 --- /dev/null +++ b/lib/telegram/bot/api.rb @@ -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 diff --git a/lib/telegram/bot/runner.rb b/lib/telegram/bot/runner.rb new file mode 100644 index 0000000..5098730 --- /dev/null +++ b/lib/telegram/bot/runner.rb @@ -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 diff --git a/lib/telegram/bot/types.rb b/lib/telegram/bot/types.rb new file mode 100644 index 0000000..7751f45 --- /dev/null +++ b/lib/telegram/bot/types.rb @@ -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' diff --git a/lib/telegram/bot/types/audio.rb b/lib/telegram/bot/types/audio.rb new file mode 100644 index 0000000..438605f --- /dev/null +++ b/lib/telegram/bot/types/audio.rb @@ -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 diff --git a/lib/telegram/bot/types/base.rb b/lib/telegram/bot/types/base.rb new file mode 100644 index 0000000..ceef170 --- /dev/null +++ b/lib/telegram/bot/types/base.rb @@ -0,0 +1,9 @@ +module Telegram + module Bot + module Types + class Base + include Virtus.model + end + end + end +end diff --git a/lib/telegram/bot/types/contact.rb b/lib/telegram/bot/types/contact.rb new file mode 100644 index 0000000..99492dd --- /dev/null +++ b/lib/telegram/bot/types/contact.rb @@ -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 diff --git a/lib/telegram/bot/types/document.rb b/lib/telegram/bot/types/document.rb new file mode 100644 index 0000000..7a8a337 --- /dev/null +++ b/lib/telegram/bot/types/document.rb @@ -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 diff --git a/lib/telegram/bot/types/group_chat.rb b/lib/telegram/bot/types/group_chat.rb new file mode 100644 index 0000000..43cc3ae --- /dev/null +++ b/lib/telegram/bot/types/group_chat.rb @@ -0,0 +1,10 @@ +module Telegram + module Bot + module Types + class GroupChat < Base + attribute :id, Integer + attribute :title, String + end + end + end +end diff --git a/lib/telegram/bot/types/location.rb b/lib/telegram/bot/types/location.rb new file mode 100644 index 0000000..1156100 --- /dev/null +++ b/lib/telegram/bot/types/location.rb @@ -0,0 +1,10 @@ +module Telegram + module Bot + module Types + class Location < Base + attribute :longitude, Float + attribute :latitude, Float + end + end + end +end diff --git a/lib/telegram/bot/types/message.rb b/lib/telegram/bot/types/message.rb new file mode 100644 index 0000000..9bf9d7b --- /dev/null +++ b/lib/telegram/bot/types/message.rb @@ -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 diff --git a/lib/telegram/bot/types/photo_size.rb b/lib/telegram/bot/types/photo_size.rb new file mode 100644 index 0000000..cba685b --- /dev/null +++ b/lib/telegram/bot/types/photo_size.rb @@ -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 diff --git a/lib/telegram/bot/types/sticker.rb b/lib/telegram/bot/types/sticker.rb new file mode 100644 index 0000000..2f30e4b --- /dev/null +++ b/lib/telegram/bot/types/sticker.rb @@ -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 diff --git a/lib/telegram/bot/types/update.rb b/lib/telegram/bot/types/update.rb new file mode 100644 index 0000000..b985740 --- /dev/null +++ b/lib/telegram/bot/types/update.rb @@ -0,0 +1,10 @@ +module Telegram + module Bot + module Types + class Update < Base + attribute :update_id, Integer + attribute :message, Message + end + end + end +end diff --git a/lib/telegram/bot/types/user.rb b/lib/telegram/bot/types/user.rb new file mode 100644 index 0000000..701e1fb --- /dev/null +++ b/lib/telegram/bot/types/user.rb @@ -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 diff --git a/lib/telegram/bot/types/video.rb b/lib/telegram/bot/types/video.rb new file mode 100644 index 0000000..5c2ac57 --- /dev/null +++ b/lib/telegram/bot/types/video.rb @@ -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 diff --git a/lib/telegram/bot/version.rb b/lib/telegram/bot/version.rb new file mode 100644 index 0000000..f308700 --- /dev/null +++ b/lib/telegram/bot/version.rb @@ -0,0 +1,5 @@ +module Telegram + module Bot + VERSION = '0.1.0' + end +end diff --git a/telegram-bot.gemspec b/telegram-bot.gemspec new file mode 100644 index 0000000..c03a384 --- /dev/null +++ b/telegram-bot.gemspec @@ -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 = ['atipugin@gmail.com'] + + 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