From 268e569d76e6cc91e74d2f7c56d231abfe26d6ad Mon Sep 17 00:00:00 2001 From: Xaloc Date: Thu, 23 Mar 2023 23:45:22 +0100 Subject: [PATCH] make bot for telegram in new branch --- auroraBot.py | 63 ++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 34 deletions(-) mode change 100644 => 100755 auroraBot.py diff --git a/auroraBot.py b/auroraBot.py old mode 100644 new mode 100755 index 1713287..8db4ee7 --- a/auroraBot.py +++ b/auroraBot.py @@ -1,54 +1,49 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- -import discord -from discord.ext import commands import config -import random +from datetime import datetime import requests TOKEN = config.token -ID = config.channel_id -intents = discord.Intents.all() +from telegram.ext import Updater +from telegram.ext import CommandHandler -bot = commands.Bot(command_prefix='!', intents=intents) +updater = Updater(token=TOKEN, use_context=True) -@bot.command(name='sky', help='Shows the latest image from the All Sky Camera in Kiruna') -async def ASC(ctx): - num = random.randint(1,100000) +dispatcher = updater.dispatcher + +def start(update, context): + context.bot.send_message(chat_id=update.effective_chat.id, text="Sóc un bot, prova a dir-me /aurora") +start_handler = CommandHandler('start', start) +dispatcher.add_handler(start_handler) + +def asc(update, ctx): + num = datetime.now().timestamp() message = f'https://www.irf.se/alis/allsky/krn/latest.jpeg?{num}' - if ctx.channel.id != ID: - await ctx.send(f"Please keep this stuff in <#{ID}>") - return - else: - await ctx.send(message) - return + ctx.bot.send_message(chat_id=update.effective_chat.id, text=message) +asc_handler = CommandHandler('aurora', asc) +dispatcher.add_handler(asc_handler) -@bot.command(name='kp', help='Shows the current Kp index') -async def kp(ctx): + +def kp(update, ctx): response = requests.get('https://services.swpc.noaa.gov/products/noaa-planetary-k-index.json') data = response.json() kp = data[-1][1] message = f'The current Kp index is {kp}' - - if ctx.channel.id != ID: - await ctx.send(f"Please keep this stuff in <#{ID}>") - return - else: - await ctx.send(message) - return + ctx.bot.send_message(chat_id=update.effective_chat.id, text=message) +kp_handler = CommandHandler('kp', kp) +dispatcher.add_handler(kp_handler) -@bot.command(name='solarWind', help='Shows graph with solar wind and magnetic field info') -async def ASC(ctx): - num = random.randint(1,100000) + +def sw(update, ctx): + num = datetime.now().timestamp() message = f'https://www.spaceweather.se/content/irf-kp.png?{num}' - if ctx.channel.id != ID: - await ctx.send(f"Please keep this stuff in <#{ID}>") - return - else: - await ctx.send(message) - return + ctx.bot.send_message(chat_id=update.effective_chat.id, text=message) +sw_handler = CommandHandler('sw', sw) +dispatcher.add_handler(sw_handler) -bot.run(TOKEN) \ No newline at end of file + +updater.start_polling() \ No newline at end of file