· 6 years ago · Mar 06, 2020, 10:16 PM
1#!/usr/bin/python
2# coding: utf8
3import logging
4import sys
5import os
6import time, datetime
7import json
8import requests
9import telegram
10from telegram.ext import CommandHandler
11from telegram.ext import MessageHandler, Filters
12from telegram.ext import Updater
13from myKeys import TGkey
14
15# logging, definizione utf-8, API key per TelegramBot e creazione files per subscribe
16logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
17level=logging.INFO)
18reload(sys)
19sys.setdefaultencoding('utf8')
20bot=telegram.Bot(token=TGkey)
21botupd=Updater(token=TGkey)
22botdis=botupd.dispatcher
23botjob=botupd.job_queue
24api='https://launchlibrary.net/1.4/launch/'
25
26# crea menu principale:
27def start(bot, update):
28 custom_keyboard = [['Prossimo Lancio', 'Prossimi 5 lanci'],
29 ['Subscribe/Unsubscribe', 'Help']]
30 markup = telegram.ReplyKeyboardMarkup(custom_keyboard,resize_keyboard=True,one_time_keyboard=True)
31 bot.send_message(chat_id=update.message.chat_id,
32 text='Menù principale',
33 reply_markup=markup)
34
35# Mostra messaggio di help:
36def Help(bot, update):
37 bot.send_message(chat_id=update.message.chat_id, text='DAIII, cos\'è che non capite!!')
38
39# Mostra prossimo lancio
40def prossimo(bot, update, n):
41 bot.send_message(chat_id=update.message.chat_id, text='Ecco le info per il prossimo lancio:')
42 req = api + 'next/5'
43 r_lanci = requests.get(req)
44 lanci = r_lanci.json()
45 msg = ''
46 msg = msg + lanci['launches'][n]['name'] + '\n\n'
47 msg = msg + 'Vettore: ' + lanci['launches'][n]['rocket']['name'] + '\n'
48 msg = msg + 'Data ed ora di lancio: ' + lanci['launches'][n]['windowstart'] + '\n'
49 msg = msg + 'Spazioporto: ' + lanci['launches'][n]['location']['pads'][0]['name'] + '\n\n'
50 msg = msg + 'Descrizione: ' + lanci['launches'][n]['missions'][0]['description'] + '\n'
51 bot.send_message(chat_id=update.message.chat_id, text=msg)
52
53# Mostra prossimi lanci
54def prossimi(bot, update):
55 bot.send_message(chat_id=update.message.chat_id, text='Ecco le date per i prossimi 5 lanci:')
56 req = api + 'next/5'
57 r_lanci = requests.get(req)
58 lanci = r_lanci.json()
59 msg = ''
60 for i in [0,1,2,3,4]:
61 msg = msg + 'Lancio numero ' + str(i + 1) + ':\n'
62 msg = msg + lanci['launches'][i]['windowstart'] + '\n'
63 msg = msg + lanci['launches'][i]['name'] + '\n\n'
64 bot.send_message(chat_id=update.message.chat_id, text=msg)
65 custom_keyboard = [['Lancio #1', 'Lancio #2'],
66 ['Lancio #3', 'Lancio #4'],
67 ['Lancio #5', 'Menù principale']]
68 markup = telegram.ReplyKeyboardMarkup(custom_keyboard,resize_keyboard=True,one_time_keyboard=True)
69 bot.send_message(chat_id=update.message.chat_id,
70 text='Dettagli lanci',
71 reply_markup=markup)
72
73# Mostra menu subscribe/unsubscribe
74def sub_menu(bot, update):
75 bot.send_message(chat_id=update.message.chat_id, text='Poi la faccio.. Prometto!!')
76
77# Decide cosa fare in base ai messaggi ricevuti:
78def echo(bot, update):
79 if update.message.text == 'Prossimo Lancio':
80 num = 0
81 prossimo(bot, update, num)
82 elif update.message.text == 'Prossimi 5 lanci':
83 prossimi(bot, update)
84 elif update.message.text == 'Subscribe/Unsubscribe':
85 sub_menu(bot, update)
86 elif update.message.text == 'Help':
87 Help(bot, update)
88 if update.message.text == 'Lancio #1':
89 num = 0
90 prossimo(bot, update, num)
91 if update.message.text == 'Lancio #2':
92 num = 1
93 prossimo(bot, update, num)
94 if update.message.text == 'Lancio #3':
95 num = 2
96 prossimo(bot, update, num)
97 if update.message.text == 'Lancio #4':
98 num = 3
99 prossimo(bot, update, num)
100 if update.message.text == 'Lancio #5':
101 num = 4
102 prossimo(bot, update, num)
103 elif update.message.text == 'Menù principale':
104 start(bot, update)
105 else:
106 pass
107
108# Funzione principale con handler telegram:
109def main():
110 start_handler = CommandHandler('start', start)
111 botdis.add_handler(start_handler)
112 help_handler = CommandHandler('help', Help)
113 botdis.add_handler(help_handler)
114 echo_handler = MessageHandler(Filters.text, echo)
115 botdis.add_handler(echo_handler)
116
117 botupd.start_polling()
118 botupd.idle()
119
120# lancio tutto da qua:
121if __name__ == '__main__':
122 main()