Class: Ayanami::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/ayanami/bot.rb

Overview

Bot is an interface for a Telegram Bot rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Bot

Returns a new instance of Bot.



9
10
11
# File 'lib/ayanami/bot.rb', line 9

def initialize(token)
  @token = token
end

Instance Method Details

#answer_callback_query(callback_query_id: nil, **args) ⇒ Object

Sends answers to callback queries sent from inline keyboards.



158
159
160
# File 'lib/ayanami/bot.rb', line 158

def answer_callback_query(callback_query_id: nil, **args)
  request('answerCallbackQuery', __grab_parameters(__method__, binding))
end

#answer_inline_query(inline_query_id: nil, results: nil, **args) ⇒ Object

Answers to an inline query. On success, True is returned.



181
182
183
# File 'lib/ayanami/bot.rb', line 181

def answer_inline_query(inline_query_id: nil, results: nil, **args)
  request('answerInlineQuery', __grab_parameters(__method__, binding))
end

#edit_message_caption(**args) ⇒ Object

Changes captions of messages sent by the bot or via the bot (for inline bots).



170
171
172
# File 'lib/ayanami/bot.rb', line 170

def edit_message_caption(**args)
  request('editMessageCaption', args)
end

#edit_message_reply_markup(**args) ⇒ Object

Changes the markup of messages sent by the bot or via the bot (for inline bots).



176
177
178
# File 'lib/ayanami/bot.rb', line 176

def edit_message_reply_markup(**args)
  request('editMessageReplyMarkup', args)
end

#edit_message_text(text: nil, **args) ⇒ Object

Changes text and game messages sent by the bot or via the bot (for inline bots).



164
165
166
# File 'lib/ayanami/bot.rb', line 164

def edit_message_text(text: nil, **args)
  request('editMessageText', __grab_parameters(__method__, binding))
end

#forward_message(chat_id: nil, from_chat_id: nil, message_id: nil, **args) ⇒ Object

Forwards a message. On success, the sent Message is returned.



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

def forward_message(chat_id: nil, from_chat_id: nil, message_id: nil,
                    **args)
  request('forwardMessage', __grab_parameters(__method__, binding))
end

#get_chat(chat_id: nil) ⇒ Object

Returns information about the chat.



138
139
140
# File 'lib/ayanami/bot.rb', line 138

def get_chat(chat_id: nil)
  request('getChat', __grab_parameters(__method__, binding))
end

#get_chat_administrators(chat_id: nil) ⇒ Object

Returns a list of administratos in a chat.



143
144
145
# File 'lib/ayanami/bot.rb', line 143

def get_chat_administrators(chat_id: nil)
  request('getChatAdministrators', __grab_parameters(__method__, binding))
end

#get_chat_member(chat_id: nil, user_id: nil) ⇒ Object

Returns an information about a member of a chat.



153
154
155
# File 'lib/ayanami/bot.rb', line 153

def get_chat_member(chat_id: nil, user_id: nil)
  request('getChatMember', __grab_parameters(__method__, binding))
end

#get_chat_members_count(chat_id: nil) ⇒ Object

Returns the number of members in a chat.



148
149
150
# File 'lib/ayanami/bot.rb', line 148

def get_chat_members_count(chat_id: nil)
  request('getChatMembersCount', __grab_parameters(__method__, binding))
end

#get_file(file_id: nil) ⇒ Object

Returns a basic info about a file and prepare it for downloading. Bots can download files up to 20 MB in size.



118
119
120
# File 'lib/ayanami/bot.rb', line 118

def get_file(file_id: nil)
  request('getFile', __grab_parameters(__method__, binding))
end

#get_meObject

Returns basic information about the bot rubocop:disable Style/AccessorMethodName



31
32
33
# File 'lib/ayanami/bot.rb', line 31

def get_me
  request('getMe')
end

#get_updates(**args) ⇒ Object

Receives incoming updates.



36
37
38
# File 'lib/ayanami/bot.rb', line 36

def get_updates(**args)
  request('getUpdates', args)
end

#get_user_profile_photos(user_id: nil, **args) ⇒ Object

Returns a list of profile pictures for a user.



112
113
114
# File 'lib/ayanami/bot.rb', line 112

def (user_id: nil, **args)
  request('getUserProfilePhotos', __grab_parameters(__method__, binding))
end

#kick_chat_member(chat_id: nil, user_id: nil) ⇒ Object

Kicks a chat member from a group/supergroup.



123
124
125
# File 'lib/ayanami/bot.rb', line 123

def kick_chat_member(chat_id: nil, user_id: nil)
  request('kickChatMember', __grab_parameters(__method__, binding))
end

#leave_chat(chat_id: nil) ⇒ Object

Leaves the group, supergroup, or channel.



128
129
130
# File 'lib/ayanami/bot.rb', line 128

def leave_chat(chat_id: nil)
  request('leaveChat', __grab_parameters(__method__, binding))
end

#request(method, **args) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/ayanami/bot.rb', line 20

def request(method, **args)
  verify_arguments(args)
  res = RestClient.post("https://api.telegram.org/bot#{@token}/#{method}",
                        args)
  return JSON.parse(res)
rescue RestClient::ExceptionWithResponse => err
  err.response
end

#send_audio(chat_id: nil, audio: nil, **args) ⇒ Object

Sends audio. On success, the sent Message is returned.



57
58
59
# File 'lib/ayanami/bot.rb', line 57

def send_audio(chat_id: nil, audio: nil, **args)
  request('sendAudio', __grab_parameters(__method__, binding))
end

#send_chat_action(chat_id: nil, action: nil) ⇒ Object

Sends a status that tells something is happening. The status is set for 5 seconds or less. Returns True on success.



107
108
109
# File 'lib/ayanami/bot.rb', line 107

def send_chat_action(chat_id: nil, action: nil)
  request('sendChatAction', __grab_parameters(__method__, binding))
end

#send_contact(chat_id: nil, phone_number: nil, first_name: nil, last_name: nil, **args) ⇒ Object

Sends phone contacts. On success, the sent Message is returned.



100
101
102
103
# File 'lib/ayanami/bot.rb', line 100

def send_contact(chat_id: nil, phone_number: nil, first_name: nil,
                 last_name: nil, **args)
  request('sendContact', __grab_parameters(__method__, binding))
end

#send_document(chat_id: nil, document: nil, **args) ⇒ Object

Sends documents. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size.



63
64
65
# File 'lib/ayanami/bot.rb', line 63

def send_document(chat_id: nil, document: nil, **args)
  request('sendDocument', __grab_parameters(__method__, binding))
end

#send_game(chat_id: nil, game_short_name: nil, **args) ⇒ Object

Sends a game. On success, the sent Message is returned.



186
187
188
# File 'lib/ayanami/bot.rb', line 186

def send_game(chat_id: nil, game_short_name: nil, **args)
  request('sendGame', __grab_parameters(__method__, binding))
end

#send_location(chat_id: nil, latitude: nil, longitude: nil, **args) ⇒ Object

Sends point on the map. On success, the sent Message is returned.



88
89
90
# File 'lib/ayanami/bot.rb', line 88

def send_location(chat_id: nil, latitude: nil, longitude: nil, **args)
  request('sendLocation', __grab_parameters(__method__, binding))
end

#send_message(chat_id: nil, text: nil, **args) ⇒ Object

Sends a Message. On success, the sent Message is returned.



41
42
43
# File 'lib/ayanami/bot.rb', line 41

def send_message(chat_id: nil, text: nil, **args)
  request('sendMessage', __grab_parameters(__method__, binding))
end

#send_photo(chat_id: nil, photo: nil, **args) ⇒ Object

Sends photos. On success, the sent Message is returned.



52
53
54
# File 'lib/ayanami/bot.rb', line 52

def send_photo(chat_id: nil, photo: nil, **args)
  request('sendPhoto', __grab_parameters(__method__, binding))
end

#send_sticker(chat_id: nil, sticker: nil, **args) ⇒ Object

Sends stickers. On success, the sent Message is returned.



68
69
70
# File 'lib/ayanami/bot.rb', line 68

def send_sticker(chat_id: nil, sticker: nil, **args)
  request('sendSticker', __grab_parameters(__method__, binding))
end

#send_venue(chat_id: nil, latitude: nil, longitude: nil, title: nil, address: nil, **args) ⇒ Object

Sends information about a venue. On success, the sent Message is returned. rubocop:disable Metrics/ParameterLists



94
95
96
97
# File 'lib/ayanami/bot.rb', line 94

def send_venue(chat_id: nil, latitude: nil, longitude: nil, title: nil,
               address: nil, **args)
  request('sendVenue', __grab_parameters(__method__, binding))
end

#send_video(chat_id: nil, video: nil, **args) ⇒ Object

Sends videos. Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size.



75
76
77
# File 'lib/ayanami/bot.rb', line 75

def send_video(chat_id: nil, video: nil, **args)
  request('sendVideo', __grab_parameters(__method__, binding))
end

#send_voice(chat_id: nil, voice: nil, **args) ⇒ Object

Sends voice messages. The audio format must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size.



83
84
85
# File 'lib/ayanami/bot.rb', line 83

def send_voice(chat_id: nil, voice: nil, **args)
  request('sendVoice', __grab_parameters(__method__, binding))
end

#set_game_score(used_id: nil, score: nil, **args) ⇒ Object

Sets the score of the specified user in a game. On success, if the message was sent by the bot, returns the edited Message, otherwise returns True. Returns an error, if the new score is not greater than the user’s current score in the chat.



194
195
196
# File 'lib/ayanami/bot.rb', line 194

def set_game_score(used_id: nil, score: nil, **args)
  request('setGameScore', __grab_parameters(__method__, binding))
end

#unban_chat_member(chat_id: nil, user_id: nil) ⇒ Object

Unbans previously kicked user in a supergroup.



133
134
135
# File 'lib/ayanami/bot.rb', line 133

def unban_chat_member(chat_id: nil, user_id: nil)
  request('unbanChatMember', __grab_parameters(__method__, binding))
end

#verify_arguments(**args) ⇒ Object



13
14
15
16
17
18
# File 'lib/ayanami/bot.rb', line 13

def verify_arguments(**args)
  args.each do |k, v|
    raise ArgumentError, "#{k} can't be nil. nil values aren't allowed"\
      if v.nil?
  end
end