Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
57 changes: 57 additions & 0 deletions bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#require 'telegram/bot'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрать.

require './lib/telegram/bot'
require './lib/tytsky'

token = '146479675:AAHmWFBCFpMCAqymM6HjUbcBBP9aHMR0bc0'

at = Tytsky.instance

answers =
Telegram::Bot::Types::ReplyKeyboardMarkup
.new(keyboard: [%w(Заклинание Рандом), %w(Задача? Стэндап?), %w(Монолог)], one_time_keyboard: false)

Telegram::Bot::Client.run(token) do |bot|
bot.listen do |message|
message.to_json
case message.text
when 'Монолог'
kb = Telegram::Bot::Types::ReplyKeyboardHide.new(hide_keyboard: true)
rand(10).times do
bot.api.send_message(chat_id: message.chat.id, text: "#{at.talk}", reply_markup: kb)
sleep(2)
end
bot.api.send_message(
chat_id: message.chat.id,
text: "#{at.talk}",
reply_markup: answers
)
when 'Рандом'
bot.api.send_message(
chat_id: message.chat.id,
text: "#{at.talk}",
reply_markup: answers
)
when 'Заклинание'
bot.api.send_message(
chat_id: message.chat.id,
text: "#{at.cast}",
reply_markup: answers
)
when 'Задача?'
bot.api.send_message(
chat_id: message.chat.id,
text: "#{at.command}",
reply_markup: answers
)
when 'Стэндап?'
bot.api.send_message(
chat_id: message.chat.id,
text: "#{at.standup_time}",
reply_markup: answers
)
when '/start'
question = 'Что вы от меня хотите?'
bot.api.send_message(chat_id: message.chat.id, text: question, reply_markup: answers)
end
end
end
10 changes: 10 additions & 0 deletions lib/telegram/bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'httmultiparty'
require 'persistent_httparty'
require 'virtus'

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

ENDPOINTS = %w(
getMe sendMessage forwardMessage sendPhoto sendAudio sendDocument
sendSticker sendVideo sendVoice sendLocation sendChatAction
getUserProfilePhotos getUpdates setWebhook getFile
).freeze
REPLY_MARKUP_TYPES = [
Telegram::Bot::Types::ReplyKeyboardMarkup,
Telegram::Bot::Types::ReplyKeyboardHide,
Telegram::Bot::Types::ForceReply
].freeze
POOL_SIZE = ENV.fetch('TELEGRAM_BOT_POOL_SIZE', 1).to_i.freeze

attr_reader :token

base_uri 'https://api.telegram.org'
persistent_connection_adapter pool_size: POOL_SIZE,
keep_alive: 30,
force_retry: true

def initialize(token)
@token = token
end

def method_missing(method_name, *args, &block)
endpoint = method_name.to_s
endpoint = camelize(endpoint) if endpoint.include?('_')

ENDPOINTS.include?(endpoint) ? call(endpoint, *args) : super
end

def call(endpoint, raw_params = {})
params = build_params(raw_params)
response = self.class.post("/bot#{token}/#{endpoint}", query: params)
if response.code == 200
response.to_hash
else
fail Exceptions::ResponseError.new(response),
'Telegram API has returned the error.'
end
end

private

def build_params(h)
h.each_with_object({}) do |(key, value), params|
params[key] = sanitize_value(value)
end
end

def sanitize_value(value)
jsonify_reply_markup(value)
end

def jsonify_reply_markup(value)
return value unless REPLY_MARKUP_TYPES.include?(value.class)
value.to_h.to_json
end

def camelize(method_name)
words = method_name.split('_')
words.drop(1).map(&:capitalize!)
words.join
end
end
end
end
21 changes: 21 additions & 0 deletions lib/telegram/bot/botan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'telegram/bot/botan/api'

module Telegram
module Bot
module Botan
attr_reader :botan

def enable_botan!(token)
@botan ||= Botan::Api.new(token)
end

def track(*args)
botan.track(*args) if defined?(botan)
end
end
end
end

if defined?(Telegram::Bot::Client)
Telegram::Bot::Client.send(:include, Telegram::Bot::Botan)
end
23 changes: 23 additions & 0 deletions lib/telegram/bot/botan/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Telegram
module Bot
module Botan
class Api
include HTTParty

attr_reader :token

base_uri 'https://api.botan.io'

def initialize(token)
@token = token
end

def track(event, uid, properties = {})
self.class.post('/track',
query: { token: token, name: event, uid: uid },
body: properties.to_json)
end
end
end
end
end
64 changes: 64 additions & 0 deletions lib/telegram/bot/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Telegram
module Bot
class Client
TIMEOUT_EXCEPTIONS = [Timeout::Error]
TIMEOUT_EXCEPTIONS << Net::ReadTimeout if Net.const_defined?(:ReadTimeout)

attr_reader :api, :offset, :timeout, :logger

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

def initialize(token, h = {})
options = default_options.merge(h)
@api = Api.new(token)
@offset = options[:offset]
@timeout = options[:timeout]
@logger = options[:logger]
end

def run
yield self
end

def listen(&block)
logger.info('Starting bot')
running = true
Signal.trap('INT') { running = false }
fetch_updates(&block) while running
exit
end

def fetch_updates
response = api.getUpdates(offset: offset, timeout: timeout)
return unless response['ok']

response['result'].each do |data|
update = Types::Update.new(data)
@offset = update.update_id.next
log_incoming_message(update.message)
yield update.message
end
rescue *TIMEOUT_EXCEPTIONS
retry
end

private

def default_options
{ offset: 0, timeout: 20, logger: NullLogger.new }
end

def log_incoming_message(message)
logger.info(
format(
'Incoming message: text="%s" uid=%i',
message.text,
message.from.id
)
)
end
end
end
end
2 changes: 2 additions & 0 deletions lib/telegram/bot/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'telegram/bot/exceptions/base'
require 'telegram/bot/exceptions/response_error'
7 changes: 7 additions & 0 deletions lib/telegram/bot/exceptions/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Telegram
module Bot
module Exceptions
class Base < StandardError; end
end
end
end
28 changes: 28 additions & 0 deletions lib/telegram/bot/exceptions/response_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Telegram
module Bot
module Exceptions
class ResponseError < Base
attr_reader :response

def initialize(response)
@response = response
end

def to_s
super +
format(' (%s)', data.map { |k, v| %(#{k}: "#{v}") }.join(', '))
end

private

def data
@data ||= begin
JSON.parse(response.body)
rescue JSON::ParserError
{ error_code: response.code, uri: response.request.last_uri.to_s }
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/telegram/bot/null_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Telegram
module Bot
class NullLogger < Logger
def initialize(*)
end

def add(*)
end
end
end
end
17 changes: 17 additions & 0 deletions lib/telegram/bot/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'telegram/bot/types/base'
require 'telegram/bot/types/user'
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/voice'
require 'telegram/bot/types/contact'
require 'telegram/bot/types/location'
require 'telegram/bot/types/chat'
require 'telegram/bot/types/message'
require 'telegram/bot/types/update'
require 'telegram/bot/types/reply_keyboard_markup'
require 'telegram/bot/types/reply_keyboard_hide'
require 'telegram/bot/types/force_reply'
require 'telegram/bot/types/file'
14 changes: 14 additions & 0 deletions lib/telegram/bot/types/audio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Telegram
module Bot
module Types
class Audio < Base
attribute :file_id, String
attribute :duration, Integer
attribute :performer, String
attribute :title, String
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
14 changes: 14 additions & 0 deletions lib/telegram/bot/types/chat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Telegram
module Bot
module Types
class Chat < Base
attribute :id, Integer
attribute :type, String
attribute :title, String
attribute :username, String
attribute :first_name, String
attribute :last_name, String
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, Integer
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
Loading