From 26c8462fd6aedd0a97335063fe21368cd93367b7 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 14 Nov 2023 14:30:50 +0100 Subject: [PATCH] Fix not sending issue --- Main.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Main.py b/Main.py index e3b9a88..87eb9a6 100644 --- a/Main.py +++ b/Main.py @@ -15,14 +15,14 @@ def get_speiseplan_tomorrow(): response = requests.get("https://mensa.mafiasi.de/api/canteens/10/tomorrow/") return response.json() -def check_for_good_stuff(BOT_TOKEN, CHAT_ID): +def check_for_good_stuff(token, chat_id): response = get_speiseplan_today() vegan = "" vegetarian = "" fav = "" if response: Header = "Der **heutige** Speiseplan " + datetime.today().strftime("%d.%m.%Y") + " ist: \n" - send_message(Header, BOT_TOKEN, CHAT_ID) + send_message(token, chat_id, Header) for meal in response: if [ele for ele in good_stuff if (ele in meal['dish'])]: fav = "⭐ " @@ -34,46 +34,49 @@ def check_for_good_stuff(BOT_TOKEN, CHAT_ID): vegan = "" vegetarian = "" fav = "" - send_message(meal_text, BOT_TOKEN, CHAT_ID) + send_message(token, chat_id, meal_text) -def send_poll(BOT_TOKEN, CHAT_ID): +def send_poll(token, chat_id): """ Send a poll to a specified Telegram chat. Args: token (str): Telegram Bot API token. - CHAT_ID (str): Chat ID where the poll is to be sent. + chat_id (str): Chat ID where the poll is to be sent. question (str): The question of the poll. options (list): A list of options for the poll. Returns: response: The response from the Telegram API. """ - url = 'https://api.telegram.org/bot' + BOT_TOKEN + '/sendPoll' + chat_id = chat_id + url = 'https://api.telegram.org/bot' + token + '/sendPoll' payload = { - 'CHAT_ID': CHAT_ID, + 'chat_id': chat_id, 'question': "Wann gehen wir heute essen?", 'options': json.dumps(["11:45", "12:00", "12:30", "13:00", "13:30"]), - 'type': 'regular', # Change this to 'quiz' if you want to send a quiz instead of a poll 'allows_multiple_answers': True, # Change this to True if you want to allow multiple answers 'is_anonymous': False # Change this to True if you want the poll to be anonymous } response = requests.get(url, data=payload) + print(response.json()) + return response.json() - -def send_message(bot_message, BOT_TOKEN, CHAT_ID): - if "&" in bot_message: - bot_message = bot_message.replace("&", "und") - send_text = 'https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage?CHAT_ID=' + CHAT_ID + '&parse_mode=Markdown&text=' + bot_message - response = requests.get(send_text) +def send_message(token, chat_id, message): + parse_mode = 'Markdown' + url = f'https://api.telegram.org/bot{token}/sendMessage' + payload = { + 'chat_id': chat_id, + 'text': message, + 'parse_mode': parse_mode + } + response = requests.post(url, json=payload) return response.json() def main(): - print("starting Bot") try: check_for_good_stuff(sys.argv[1],sys.argv[2]) - # check_for_good_stuff(token, CHAT_ID) send_poll(sys.argv[1],sys.argv[2]) except UnboundLocalError: print("Error: Please provide a valid token and chat id")