-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
75 lines (64 loc) · 2.74 KB
/
bot.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from cachama_templates import new_cachama, old_cachama
import telebot, random, datetime, time
DEBUG = True
LAST_CALL_PERIOD = 1*3600
engine = create_engine("sqlite:///users.db",echo=True)
Session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer,nullable=False,primary_key=True)
username = Column(String)
cachamas = relationship("Cachama",backref="user",uselist=False)
class Cachama(Base):
__tablename__ = 'cachama'
id = Column(Integer,ForeignKey("user.id"),nullable=False,primary_key=True)
total = Column(Integer,default=0)
last_call= Column(DateTime)
Base.metadata.create_all(engine)
with open("token.txt") as f:
token = f.read()
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def startMessage(message):
bot.send_message(message.chat.id,"Soy el bot de laboratorio de prototipos")
@bot.message_handler(commands=['cachama','cachamas'])
def cachama(message):
session = Session()
if (message.chat.type == 'private' or message.chat.type == 'channel') and DEBUG == False:
return
user = session.query(User).filter(User.id == message.from_user.id).one_or_none()
if user is None:
user = User(id=message.from_user.id,username=message.from_user.username)
session.add(user)
cachama = Cachama(id=message.from_user.id,total=0,last_call=datetime.datetime.utcfromtimestamp(time.time()-(LAST_CALL_PERIOD+1)))
session.add(cachama)
session.flush()
if user.username is None:
user.username = message.from_user.username
session.flush()
print user.username, " agregado"
delta = datetime.datetime.now() - user.cachamas.last_call
if delta.seconds > LAST_CALL_PERIOD:
new_cachamas = generate_new_cachamas()
user.cachamas.total = user.cachamas.total+new_cachamas
user.cachamas.last_call = datetime.datetime.now()
response = random.choice(new_cachama).format(name=message.from_user.first_name,new=new_cachamas,total=user.cachamas.total)
session.commit()
else:
response = random.choice(old_cachama).format(name=message.from_user.first_name,total=user.cachamas.total)
session.commit()
pass
Session.remove()
bot.send_message(message.chat.id,response)
def new_user(id_new):
user = User(id=id_new)
session.add(user)
session.commit()
def generate_new_cachamas():
return (10*random.randint(0,9))+random.randint(0,9)
if __name__ == "__main__":
bot.polling()