Skip to content

Commit

Permalink
Finish updating to bot API 4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
watzon committed Apr 26, 2020
1 parent a93ef6e commit 319acc0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 8 deletions.
7 changes: 6 additions & 1 deletion examples/dice_bot.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ require "../src/tourmaline"
class DiceBot < Tourmaline::Client
@[Command("roll")]
def roll_command(client, update)
update.message.try &.reply_with_dice()
update.message.try &.reply_with_dice
end

@[Command("throw")]
def throw_command(client, update)
update.message.try &.reply_with_dart
end
end

Expand Down
31 changes: 27 additions & 4 deletions src/tourmaline/client/core.cr
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,33 @@ module Tourmaline
reply_to_message_id = reply_to_message.is_a?(Int32 | Int64 | Nil) ? reply_to_message : reply_to_message.id

response = request("sendDice", {
chat_id: chat_id,
chat_id: chat_id,
emoji: "🎲",
disable_notification: disable_notification,
reply_to_message_id: reply_to_message_id,
reply_markup: reply_markup,
})

Message.from_json(response)
end

# Use this method to send a dart animation, which works as a random "heads or tails"
# kind of game.
def send_dart(
chat,
disable_notification = false,
reply_to_message = nil,
reply_markup = nil
)
chat_id = chat.is_a?(Int) ? chat : chat.id
reply_to_message_id = reply_to_message.is_a?(Int32 | Int64 | Nil) ? reply_to_message : reply_to_message.id

response = request("sendDice", {
chat_id: chat_id,
emoji: "🎯",
disable_notification: disable_notification,
reply_to_message_id: reply_to_message_id,
reply_markup: reply_markup
reply_to_message_id: reply_to_message_id,
reply_markup: reply_markup,
})

Message.from_json(response)
Expand Down Expand Up @@ -1124,7 +1147,7 @@ module Tourmaline
# commands = commands.map(&.to_h.transform_keys(&.to_s))

response = request("setMyCommands", {
commands: commands
commands: commands,
})

response == "true"
Expand Down
5 changes: 4 additions & 1 deletion src/tourmaline/helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ module Tourmaline
actions << UpdateAction::ConnectedWebsite if message.connected_website
# actions << UpdateAction::PassportData if message.passport_data
actions << UpdateAction::Poll if message.poll
actions << UpdateAction::Dice if message.dice
if dice = message.dice
actions << UpdateAction::Dice if dice.emoji == "🎲"
actions << UpdateAction::Dart if dice.emoji == "🎯"
end
end

actions << UpdateAction::EditedMessage if update.edited_message
Expand Down
1 change: 1 addition & 0 deletions src/tourmaline/models/dice.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Tourmaline
class Dice
include JSON::Serializable

getter emoji : String
getter value : Int32
end
end
2 changes: 1 addition & 1 deletion src/tourmaline/models/message.cr
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ module Tourmaline
Container.client.send_message(chat, message, **kwargs, reply_to_message: nil)
end

{% for content_type in %w[audio animation contact document location photo media_group venu video video_note voice invoice poll dice] %}
{% for content_type in %w[audio animation contact document location photo media_group venu video video_note voice invoice poll dice dart] %}
def reply_with_{{content_type.id}}(*args, **kwargs)
Container.client.send_{{content_type.id}}(chat, *args, **kwargs, reply_to_message: message_id)
end
Expand Down
2 changes: 1 addition & 1 deletion src/tourmaline/models/poll.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Tourmaline

getter correct_option_id : Int32?

getter explanation_entities : Array(MessageEntity) = [] of MessageEntity
getter explanation_entities : Array(Tourmaline::MessageEntity) = [] of Tourmaline::MessageEntity
# :nodoc:
module PollTypeConverter
def self.from_json(value : JSON::PullParser)
Expand Down
2 changes: 2 additions & 0 deletions src/tourmaline/persistence.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ module Tourmaline
abstract def persistent_cleanup
end
end

require "./persistence/nil_persistence"
1 change: 1 addition & 0 deletions src/tourmaline/update_action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Tourmaline
Poll
PollAnswer
Dice
Dart

def to_s
super.to_s.underscore
Expand Down

0 comments on commit 319acc0

Please sign in to comment.