From 64f894c41eb3c91fdeca4459df05e7eda8928cd6 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Fri, 28 Dec 2018 01:22:35 -0600 Subject: [PATCH] added more message validation to protect against crashes --- commands.rb | 3 ++- run.rb | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/commands.rb b/commands.rb index 2cdc181..4de966b 100644 --- a/commands.rb +++ b/commands.rb @@ -1,7 +1,8 @@ def validate_incoming_data(message) + auth_chat = @conf['auth_chat'] message = message.message if message.is_a? Telegram::Bot::Types::CallbackQuery return "Received message is not from a valid source! Type: \"#{message.chat.type}\". Ignoring." if ! @allowed_sources.include?(message.chat.type) - return "Unauthorized user sent message. User ID: #{message.from.id} Source ID: #{message.chat.id}." if ! @conf['auth_chat'].include?(message.chat.id.to_s) + #return "Unauthorized user sent message. User ID: #{message.from.id} Source ID: #{message.chat.id}." if ! auth_chat.include?(message.chat.id.to_s) #puts message.chat.type return true end diff --git a/run.rb b/run.rb index 1bb873f..6ccb8a2 100755 --- a/run.rb +++ b/run.rb @@ -64,6 +64,22 @@ puts "Bot token: #{token}\n\n\n\n" #end def handle_message(message) + if ! message.reply_to_message.nil? then + #drop message. Someone's replying to a message + #sent by our bot + return + end + + if message.text.nil? + # Find out if user(s) joined the group. If so, welcome them + #if ! message.new_chat_members.nil? + # handle_user_join(message) + #else + # #Handle non-messages and non-joins here + #end + return #so that we don't try to process this as a command (below) + end + #Format sender name if ! message.from.username.nil? message.from.username = "@" + message.from.username @@ -150,7 +166,7 @@ Telegram::Bot::Client.run(token) do |bot| reply = handle_message(message) if reply == "dance" || reply == "flex" bot.api.sendVideo(chat_id: message.chat.id, video: "https://img.skyfalltech.net/togra/#{reply}.gif") - else + elsif ! message.text.nil? bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") end else