Class: TelegramBot

Inherits:
Object
  • Object
show all
Includes:
EventHandler, Request, RequestMethods
Defined in:
lib/telegram_bot.rb,
lib/telegram_bot/handler.rb,
lib/telegram_bot/matcher.rb,
lib/telegram_bot/request.rb,
lib/telegram_bot/version.rb,
lib/telegram_bot/blank_slate.rb,
lib/telegram_bot/poll_listener.rb,
lib/telegram_bot/request_methods.rb,
lib/telegram_bot/auto_from_methods.rb

Defined Under Namespace

Modules: AutoFromMethods, EventHandler, Request, RequestMethods, ShorthandMethods Classes: AnythingMatcher, Audio, BlankSlate, Chat, CommandMatcher, Contact, Date, Document, FallbackMatcher, ForceReply, GroupChat, Location, Matcher, Message, PhotoSize, PollListener, ReplyKeyboardHide, ReplyKeyboardMarkup, Sticker, TextMatcher, Update, User, Video

Constant Summary collapse

VERSION =
"0.1.3"

Constants included from RequestMethods

RequestMethods::CHAT_ACTIONS, RequestMethods::METHODS

Constants included from Request

Request::API_BASE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestMethods

#forward_message, #get_me, #get_updates, #send_chat_action, #send_message

Methods included from Request

included, #request

Methods included from EventHandler

#handle, included, #on

Constructor Details

#initialize(history: 50) ⇒ TelegramBot

Returns a new instance of TelegramBot.



17
18
19
20
# File 'lib/telegram_bot.rb', line 17

def initialize(history: 50)
  @history = []
  @history_length = 50
end

Instance Attribute Details

#historyObject

Returns the value of attribute history.



15
16
17
# File 'lib/telegram_bot.rb', line 15

def history
  @history
end

Instance Method Details

#append_history(message) ⇒ Object



46
47
48
49
# File 'lib/telegram_bot.rb', line 46

def append_history(message)
  @history << message
  @history = @history.last(@history_length || 0)
end

#extend_env(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/telegram_bot.rb', line 30

def extend_env(env)
  telegram_bot = self
  env.extend do
    define_method :bot do
      telegram_bot
    end
    alias_method :client, :bot

    define_method :history do
      telegram_bot.history
    end

    include ShorthandMethods
  end
end

#listen(method: :poll, interval: 5, path: '/hook') ⇒ Object



22
23
24
25
26
27
28
# File 'lib/telegram_bot.rb', line 22

def listen(method: :poll, interval: 5, path: '/hook')
  @listen = {
    method:   method,
    interval: interval,
    path:     path
  }
end

#start!Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/telegram_bot.rb', line 51

def start!
  listener = nil
  case @listen[:method]
  when :poll
    listener = PollListener.new(self, @listen[:interval])
  when :webhook
    warn 'not implemented'
  end
  listener.start!
end