Skip to content

Commit 89a37e1

Browse files
committed
bot as model
1 parent 3250dfe commit 89a37e1

File tree

2 files changed

+19
-45
lines changed

2 files changed

+19
-45
lines changed
Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,10 @@
11
from django.core.management.base import BaseCommand, CommandError
2-
from medios_libres.models import Texto, Ubicacion
3-
import telebot
4-
from telebot import types
5-
6-
# token medios libres
7-
token = "317988596:AAEH2w1RxDYg26hf0aVVFuqtscM7d8dr98Q"
2+
from medios_libres.models import MediosBot
83

94
class Command(BaseCommand):
10-
help = 'Listen to Telegram Bot in the background'
5+
help = """Listen to Telegram Bot in the background
6+
(medios_venv)$ python manage.py bot_listen"""
117

128
def handle(self, *args, **options):
13-
bot = telebot.TeleBot(token)
14-
15-
geo = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
16-
boton = types.KeyboardButton('Enviar posicion', request_location=True)
17-
geo.add(boton)
18-
19-
@bot.message_handler(commands=['monitor',])
20-
def mandar_posicion(message):
21-
"""al comando /monitor responde con un teclado con el boton para enviar ubicacion"""
22-
cid = message.chat.id
23-
bot.send_message(cid, "Reportar mi ubicacion", reply_markup=geo)
24-
25-
@bot.message_handler(content_types=['location',])
26-
def recibir_posicion(message):
27-
user=message.from_user
28-
if user.username:
29-
username = user.username
30-
elif user.first_name:
31-
username = user.first_name
32-
elif user.last_name:
33-
username = user.last_name
34-
latitude = message.location.latitude
35-
longitude = message.location.longitude
36-
try:
37-
Ubicacion.objects.create(lat=latitude, longi=longitude, usuario=username)
38-
except Exception:
39-
print("falla lat:{}, long:{}, user:{}".format(latitude, longitude, username))
40-
# TODO FWD
41-
42-
bot.polling()
9+
medios = MediosBot()
10+
medios.bot.polling()

medios_libres/medios_libres/models.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
from django.db import models
2+
import telebot
3+
from django.conf import settings
24

3-
# DJANGO
5+
class MediosBot:
46

5-
class Texto(models.Model):
6-
texto = models.TextField()
7+
bot = telebot.TeleBot('')
78

8-
class Ubicacion(models.Model):
9-
lat = models.FloatField()
10-
longi = models.FloatField()
11-
usuario = models.CharField(max_length=50) # TODO model
12-
tiempo = models.DateTimeField(auto_now=True) # redundante?
9+
def __init__(self, token=None):
10+
if token:
11+
self.bot.token = token
12+
13+
@bot.message_handler(content_types=['text',])# commands=['publicar',]
14+
def listen_text(message):
15+
wp_post = WpPosts(
16+
post_content=message.text
17+
)
18+
wp_post.save()
1319

1420
# WORDPRESS unmanaged
1521
# obtained by inspectdb

0 commit comments

Comments
 (0)