Nestor

Mon petit bot Discord
Log | Files | Refs

commit 05480f32ec36fbac0961f1e469e769f860b828c0
parent 213dbaa32136983e764b9e70213788c3b4dddc66
Author: Doshirae <doshirae@doshirae.fr>
Date:   Tue,  9 Oct 2018 21:24:48 +0200

Latex command

Diffstat:
Nestor.rb | 9++++++++-
Nestor_telegram.rb | 57---------------------------------------------------------
commandes.rb | 22++++++++++++++++++++++
3 files changed, 30 insertions(+), 58 deletions(-)

diff --git a/Nestor.rb b/Nestor.rb @@ -12,7 +12,6 @@ $ALLOWED = { nico: 447878477044121620, hugo: 447867234371764249 } - # This statement creates a bot with the specified token and application ID. After this line, you can add events to the # created bot, and eventually run it. bot = Discordrb::Commands::CommandBot.new token: configatron.token, client_id: 261161348124114945, prefix: '!' @@ -188,6 +187,14 @@ bot.command(:kitten) do |event| end # }}} +# latex {{{ +bot.command(:latex, description: "Renvoie une image avec du LaTeX [EXPERIMENTAL, n'utiliser que du latex, pas d'unicode]", usage: "!latex <latex>") do |event, *latex| + latex(latex) + event.channel.send_file File.new('latex.png') + `rm latex.png` +end +# }}} + # exit {{{ bot.command(:exit, help_available: false) do |event| # This is a check that only allows a user with a specific ID to execute this command. Otherwise, everyone would be diff --git a/Nestor_telegram.rb b/Nestor_telegram.rb @@ -1,57 +0,0 @@ -require 'telegram/bot' -require_relative "commandes.rb" -require "configatron" -require_relative "config.rb" - -token = configatron.telegram -Telegram::Bot::Client.run(token) do |bot| - bot.listen do |message| - case message.text - when "/doggo" - doggo() - bot.api.send_photo(chat_id: message.chat.id, photo: Faraday::UploadIO.new('doggo.png', 'image/png')) - when "/kitten" - kitten() - bot.api.send_photo(chat_id: message.chat.id, photo: Faraday::UploadIO.new('kitten.png', 'image/png')) - else - bot.api.send_message(chat_id: message.chat.id, text: "Commande inconnue") - end - end -end - -=begin Exemples -# Custom keyboard -bot.listen do |message| - case message.text - when '/start' - question = 'London is a capital of which country?' - # See more: https://core.telegram.org/bots/api#replykeyboardmarkup - answers = - Telegram::Bot::Types::ReplyKeyboardMarkup - .new(keyboard: [%w(A B), %w(C D)], one_time_keyboard: true) - bot.api.send_message(chat_id: message.chat.id, text: question, reply_markup: answers) - when '/stop' - # See more: https://core.telegram.org/bots/api#replykeyboardremove - kb = Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true) - bot.api.send_message(chat_id: message.chat.id, text: 'Sorry to see you go :(', reply_markup: kb) - end -end -# Inline keyboards -bot.listen do |message| - case message - when Telegram::Bot::Types::CallbackQuery - # Here you can handle your callbacks from inline buttons - if message.data == 'touch' - bot.api.send_message(chat_id: message.from.id, text: "Don't touch me!") - end - when Telegram::Bot::Types::Message - kb = [ - Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Go to Google', url: 'https://google.com'), - Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Touch me', callback_data: 'touch'), - Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Switch to inline', switch_inline_query: 'some text') - ] - markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb) - bot.api.send_message(chat_id: message.chat.id, text: 'Make a choice', reply_markup: markup) - end -end -=end diff --git a/commandes.rb b/commandes.rb @@ -65,3 +65,25 @@ end def dl(img) `curl -o img.png #{img}` end + + +def render_latex(latex) +end + + +def latex(*args) + def download_file(file) + system(%Q(wget -O latex.png "http://rtex.probablyaweb.site/api/v2/#{file}")) + end + + latex = args.join(" ") + uri = URI("http://rtex.probablyaweb.site/api/v2") + payload = { 'code' => latex, 'format' => 'png' }.to_json + req = Net::HTTP.post(uri, payload, "Content-Type" => "application/json") + response = JSON.parse(req.body) + if response["status"] == "error" + raise "status == error" + end + file = response["filename"] + download_file(file) +end