-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot3.py
54 lines (43 loc) · 1.38 KB
/
bot3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from telegram.ext import Updater, CommandHandler
def fib(n):
'''calcula l'n-èsim nombre de fibonacci'''
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a
def start(update, context):
context.bot.send_message(
chat_id=update.effective_chat.id,
text="Hola! Soc un bot amb comandes /suma x y i /fibo n on x,y i n són nombres.")
def suma(update, context):
try:
x = float(context.args[0])
y = float(context.args[1])
s = x + y
context.bot.send_message(
chat_id=update.effective_chat.id,
text=str(s))
except Exception as e:
print(e)
context.bot.send_message(
chat_id=update.effective_chat.id,
text='💣')
def fibo(update, context):
try:
n = int(context.args[0])
f = fib(n)
context.bot.send_message(
chat_id=update.effective_chat.id,
text=str(f))
except Exception as e:
print(e)
context.bot.send_message(
chat_id=update.effective_chat.id,
text='💣')
TOKEN = open('token.txt').read().strip()
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('suma', suma))
dispatcher.add_handler(CommandHandler('fibo', fibo))
updater.start_polling()