Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ venv.bak/

# mypy
.mypy_cache/


# personal things
*.txt
rhea.config.yaml
tarazed.config.yaml
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ listeners:
telegram:
api_token: <token>
chat_id: <chat_id>
discord:
webhook_url: <webhook_url>
bot:
listeners: [telegram]
```
Expand Down
4 changes: 3 additions & 1 deletion bot/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
import yaml

from bot.listeners import TelegramListener, AlertListener
from bot.listeners import TelegramListener, DiscordListener, AlertListener
from bot.protocol import SendExpedition
from ogame.game.const import Ship, CoordsType, Resource
from ogame.game.model import Coordinates
Expand Down Expand Up @@ -109,6 +109,8 @@ def load_config(file):
def _initialize_listener(name, config):
if name == 'telegram':
return TelegramListener(**config)
elif name == 'discord':
return DiscordListener(**config)
elif name == 'alert':
return AlertListener(**config)
else:
Expand Down
33 changes: 33 additions & 0 deletions bot/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,40 @@ def _send_message_url(self):
def _api_url(self):
return f'https://api.telegram.org/bot{self.api_token}'

class DiscordListener(Listener):
def __init__(self, webhook_url):
self.webhook_url = webhook_url

def notify(self, notification):
message = parse_notification(notification)
if message:
# message = self._escape_markdown_string(message)
self._send_message(message)

def notify_exception(self, exception):
# print(exception)
message = parse_exception(exception)
self._send_message(message)

def _send_message(self, message):
try:
response = requests.post(
self.webhook_url,
timeout=5,
data={'content': message})
if not response.status_code == 204:
response = response.json()
logging.error(f'Failed to send discord message: {response.get("message")}')
except requests.exceptions.RequestException:
logging.exception('Exception thrown while sending a discord message.')
except ValueError:
logging.exception('Exception thrown while parsing the response.')

@staticmethod
def _escape_markdown_string(string):
for c in '_[]()~>#+-=|{}.!':
string = string.replace(c, f'\\{c}')
return string
class AlertListener(Listener):
def __init__(self, wakeup_wav=None, error_wav=None):
self._check_wav_file(wakeup_wav, raise_exc=True)
Expand Down
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ listeners:
telegram:
api_token: <API token> # unique authentication token of the telegram bot; see https://core.telegram.org/bots/api
chat_id: <chat id> # id of the chat between you and the telegram bot
discord:
webhook_url: # discord webhook url
alert:
wakeup_wav: # path to .wav file which will be played on every wake-up
error_wav: # path to .wav file which will be played on every exception
Expand Down
27 changes: 26 additions & 1 deletion ogame/game/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ def get_fleet_dispatch(self,
planet: Union[Planet, int],
delay: int = None) -> FleetDispatch:
fleet_dispatch_soup = self._get_fleet_dispatch(planet, delay=delay)
token = find_first_between(str(fleet_dispatch_soup), left='fleetSendingToken = "', right='"')
# token = find_first_between(str(fleet_dispatch_soup), left='fleetSendingToken = "', right='"')
token = find_first_between(str(fleet_dispatch_soup), left='var token = "', right='"')

timestamp = int(fleet_dispatch_soup.find('meta', {'name': 'ogame-timestamp'})['content'])
slot_elements = fleet_dispatch_soup.find(id='slots').findAll('div', recursive=False)
used_fleet_slots, max_fleet_slots = extract_numbers(slot_elements[0].text)
Expand Down Expand Up @@ -679,9 +681,32 @@ def _get_event_list(self,
headers={'X-Requested-With': 'XMLHttpRequest'},
delay=delay)

def _check_fleet_dispatch(self,
fleet_dispatch_data,
delay: int = None):
return self._post_game_resource(
resource='json',
params={'page': 'ingame',
'component': 'fleetdispatch',
'action': 'checkTarget',
'ajax': 1,
'asJson': 1},
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'},
data=fleet_dispatch_data,
delay=delay)

def _post_fleet_dispatch(self,
fleet_dispatch_data,
delay: int = None):

checkRes = self._check_fleet_dispatch(fleet_dispatch_data, delay)
if checkRes["status"] != 'success':
raise ValueError(checkRes["status"])

newAjaxToken = checkRes["newAjaxToken"]
fleet_dispatch_data["token"] = newAjaxToken

return self._post_game_resource(
resource='json',
params={'page': 'ingame',
Expand Down
1 change: 1 addition & 0 deletions ogame/game/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Mission(IdEnum):
colonization = 7
harvest = 8
destroy = 9
missile = 10
expedition = 15
trade = 16

Expand Down