Skip to content

Instantly share code, notes, and snippets.

@xuio
Forked from pbock/buergerbot.rb
Last active October 9, 2024 06:12
Show Gist options
  • Save xuio/297e16b84d700314241393e8f3aa42f3 to your computer and use it in GitHub Desktop.
Save xuio/297e16b84d700314241393e8f3aa42f3 to your computer and use it in GitHub Desktop.
Bürgerbot: Refreshes the Berlin Bürgeramt page until an appointment becomes available, then notifies you via Telegram
#!/usr/bin/env ruby
require 'watir-webdriver'
require 'telegram/bot'
Telegram.bots_config = {
default: '<Telegram Bot token>',
}
Telegram.bot.get_updates
chat_id = '<Telegram Chat ID>'
def log (message) puts " #{message}" end
def success (message) puts "+ #{message}" end
def fail (message) puts "- #{message}" end
def notify (message)
success message.upcase
system 'osascript -e \'Display notification "Bürgerbot" with title "%s"\'' % message
rescue StandardError => e
end
def appointmentAvailable? (b)
url = 'https://service.berlin.de/terminvereinbarung/termin/tag.php?termin=1&dienstleister=122282&anliegen[]=120703&herkunft=1'
puts '-'*80
log 'Trying again'
b.goto url
log 'Page loaded'
link = b.element css: '.calendar-month-table:first-child td.buchbar a'
if link.exists?
link.click
notify 'An appointment is available.'
log 'Enter y to keep searching or anything else to quit.'
Telegram.bot.send_message(chat_id: chat_id, text: 'Found an appointment! Book here: https://service.berlin.de/terminvereinbarung/termin/tag.php?termin=1&dienstleister=122282&anliegen[]=120703&herkunft=1')
return gets.chomp.downcase != 'y'
else
fail 'No luck this time.'
return false
end
rescue StandardError => e
fail 'Error encountered.'
puts e.inspect
return false
end
b = Watir::Browser.new
Telegram.bot.send_message(chat_id: chat_id, text: 'starting bot...')
until appointmentAvailable? b
log 'Sleeping.'
sleep 60
end
@davidfurlong
Copy link

davidfurlong commented May 5, 2021

Guide:

  1. Create a telegram bot (google it)
  2. Write message to your bot
  3. Run the below curl command with your telegram token

$ curl https://api.telegram.org/bot[TOKEN]/getUpdates

Take this "id" from the curl here XYXYYXYX and paste it into file where it says chat id. Also paste auth token into file above where it says

$ gem install watir
$ gem install telegram/bot

Make sure the correct version of chromedriver (matching your installed chrome version) is installed (brew install chromedriver) and is in your $PATH

$ ruby buergerbot.rb

Change code from 'watir-webdriver' to 'watir'
watir-webdriver is deprecated in favour of watir for anyone else who uses this and has issues

@davidfurlong
Copy link

Also you are likely to get rate-limited as I did

@maxlembke
Copy link

Unbelievable that I got to learn Ruby to book an appointment - just to relocate back to Berlin. Let's see if I can get this working and thanks for the sharing on git!

@akshygupt
Copy link

I am getting a not found error when installing telegram/bot, any help would be appreciated?

@MDIB
Copy link

MDIB commented Jun 8, 2021

@akshygupt I've managed to get it working by running gem install telegram-bot

@noslav
Copy link

noslav commented Aug 4, 2021

confirmed this works! just watch out to not get rate limited <60 seconds wait time as @davidfurlong said, thanks for the additional tips on getting all the dependencies installed.

@n0rtap
Copy link

n0rtap commented Sep 1, 2021

Thank you very much for this! Somehow I only manage to get telegram notifications, when starting the bot.
I don't get notified via telegram when an appointment is availbe. In console I it works fine though.
Any ideas? @davidfurlong

Error is:
#<NameError: undefined local variable or method `chat_id' for main:Object>

@Runflast
Copy link

Runflast commented Sep 9, 2021

Hej, thanks for sharing! Got the same problem as @n0rtap and the same Error.

I´m a total newbee so i wanted to share some (maybe pretty obvious stuff) for other like me:
How do you let the bot search for the service you need and at the location you prefer?

  1. Open up the main side for your service (e.x.: https://service.berlin.de/dienstleistung/120335/)
  2. There to ways now: A) If you like to search "Berlinweit" you just copy the link of the blue button saying "Termine berlinweit suchen" B) If you like to search only at "your" Bürgeramt you scroll down and copy the link for your Bürgeramt.
  3. Pass it in where url stands (carfull both times!)

Hope that helps.
Looking forward to a solution for the other problem.

@Runflast
Copy link

Runflast commented Sep 9, 2021

Thank you very much for this! Somehow I only manage to get telegram notifications, when starting the bot.
I don't get notified via telegram when an appointment is availbe. In console I it works fine though.
Any ideas? @davidfurlong

Error is:
#<NameError: undefined local variable or method `chat_id' for main:Object>

I found a work around. Just pass in your id direcly like:
Telegram.bot.send_message(chat_id: 32912731, text: 'Found an appointment! Book here: https://service.berlin.de/terminvereinbarung/termin/tag.php?termin=1&dienstleister=122282&anliegen[]=120703&herkunft=1')

Hope that helps. Good luck!

@nicbou
Copy link

nicbou commented Jan 25, 2022

I've built a similar tool, but it updates a public webpage via websockets

@skiwlkr
Copy link

skiwlkr commented Jan 31, 2022

Thanks for your help @Runflast
I got everything working but couldn't figure out from where I get the correct link.
This really helped!

@nicbou
Great idea! That could have helped a lot of people.

I'm running it now for several hours and although sometimes a free spot appears the bot is not triggered. I wonder if this got to do with the Captcha that pops up when you click manually on it.

@tolgaijin
Copy link

Have the same issue with the captcha as @skiwlkr

@nicbou you took your tool down. Would be cool if you could publish a how-to or your code - for educational purposes ;)

@Ruperr
Copy link

Ruperr commented Apr 2, 2022

Is there any way to get rid of the captchas when a new appointment appears?

@nicbou
Copy link

nicbou commented Apr 2, 2022

They are here for a reason. Have a look at their robots.txt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment