mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-03-18 09:55:57 +00:00
Compare commits
1 Commits
assets_leg
...
telegram
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b963beeded |
@@ -44,6 +44,7 @@ INSTALLED_APPS = (
|
|||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'RIGS',
|
'RIGS',
|
||||||
|
'telegram_interface',
|
||||||
|
|
||||||
'debug_toolbar',
|
'debug_toolbar',
|
||||||
'registration',
|
'registration',
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
from django.db import models
|
|
||||||
import reversion
|
|
||||||
|
|
||||||
|
|
||||||
# Create your models here.
|
|
||||||
class Suppliers(models.Model):
|
|
||||||
name = models.CharField(max_length=100)
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractAsset(models.Model):
|
|
||||||
STATUS_LOST = 1
|
|
||||||
STATUS_ACTIVE = 2
|
|
||||||
STATUS_INACTIVE = 3
|
|
||||||
STATUS_BROKEN = 4
|
|
||||||
STATUS_SCRAPPED = 5
|
|
||||||
STATUS_NOT_BUILT = 6
|
|
||||||
STATUS_SOLD = 7
|
|
||||||
|
|
||||||
STATUS_CHOICES = (
|
|
||||||
(STATUS_LOST, 'Lost'),
|
|
||||||
(STATUS_ACTIVE, 'Active'),
|
|
||||||
(STATUS_INACTIVE, 'Inactive'),
|
|
||||||
(STATUS_BROKEN, 'Broken'),
|
|
||||||
(STATUS_SCRAPPED, 'Scrapped'),
|
|
||||||
(STATUS_NOT_BUILT, 'Not Yet Built'),
|
|
||||||
(STATUS_SOLD, 'Sold'),
|
|
||||||
)
|
|
||||||
|
|
||||||
status = models.IntegerField(choices=STATUS_CHOICES)
|
|
||||||
notes = models.TextField(blank=True, null=True)
|
|
||||||
# test_period # decide what to do with this later
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
abstract = True
|
|
||||||
|
|
||||||
|
|
||||||
class Asset(models.Model):
|
|
||||||
CATEGORY_GENERAL = 1
|
|
||||||
CATEGORY_CASE = 2
|
|
||||||
CATEGORY_COMMS = 3
|
|
||||||
CATEGORY_DECKING = 4
|
|
||||||
CATEGORY_OFFICE = 5
|
|
||||||
CATEGORY_SOUND = 10
|
|
||||||
CATEGORY_LIGHTING = 20
|
|
||||||
CATEGORY_VIDEO = 30
|
|
||||||
CATEGORY_RIGGING = 40
|
|
||||||
CATEGORY_TRUSS = 41
|
|
||||||
CATEGORY_LADDERS = 42
|
|
||||||
CATEGORY_POWER = 50
|
|
||||||
CATEGORY_DISTRO = 51
|
|
||||||
|
|
||||||
CATEGORY_CHOICES = (
|
|
||||||
(CATEGORY_SOUND, 'Sound'),
|
|
||||||
(CATEGORY_LIGHTING, 'Lighting'),
|
|
||||||
('Other', (
|
|
||||||
(CATEGORY_GENERAL, 'General'),
|
|
||||||
(CATEGORY_CASE, 'Case'),
|
|
||||||
(CATEGORY_COMMS, 'Comms'),
|
|
||||||
(CATEGORY_DECKING, 'Decking'),
|
|
||||||
(CATEGORY_OFFICE, 'Office'),
|
|
||||||
)),
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
||||||
name = models.CharField(max_length=255)
|
|
||||||
serial_number = models.CharField(max_length=255)
|
|
||||||
date_acquired = models.DateField(null=True, blank=True)
|
|
||||||
date_sold = models.DateField(null=True, blank=True)
|
|
||||||
purchase_price = models.DecimalField(max_digits=10, decimal_places=2)
|
|
||||||
replacement_price = models.DecimalField(max_digits=10, decimal_places=2)
|
|
||||||
|
|
||||||
82
telegram_interface/models.py
Normal file
82
telegram_interface/models.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
import telegram
|
||||||
|
import time
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
class TelegramManager(models.Model):
|
||||||
|
offset = models.CharField(max_length=255, null=False, blank=True, default="")
|
||||||
|
token = models.CharField(max_length=255, null=False, blank=False)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def bot(self):
|
||||||
|
return telegram.Bot(token=self.token)
|
||||||
|
|
||||||
|
def lookupUpdates(self):
|
||||||
|
updates = self.bot.getUpdates(offset=self.offset)
|
||||||
|
if len(updates) > 0:
|
||||||
|
self.parseUpdates(updates)
|
||||||
|
self.offset = updates[-1].to_dict()["update_id"]+1
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
def parseUpdates(self,updates):
|
||||||
|
for update in updates:
|
||||||
|
chat_id = update.message.chat_id
|
||||||
|
|
||||||
|
try:
|
||||||
|
chat_object = Chat.objects.get(chat_id=chat_id,manager=self)
|
||||||
|
self.knownChat(update,chat_object)
|
||||||
|
|
||||||
|
except Chat.DoesNotExist:
|
||||||
|
self.unknownChat(update)
|
||||||
|
|
||||||
|
def unknownChat(self,update):
|
||||||
|
text = update.message.text
|
||||||
|
|
||||||
|
if text.startswith("/start"):
|
||||||
|
chat = Chat(chat_id=update.message.chat.id,manager=self)
|
||||||
|
chat.save()
|
||||||
|
|
||||||
|
chat.sendMessage( "Hi, I'll send you updates on any rigs.")
|
||||||
|
else:
|
||||||
|
self.bot.sendMessage(chat_id=update.message.chat.id, text="I don't know you, and won't talk to you until you /start me.")
|
||||||
|
|
||||||
|
def knownChat(self, update, chat):
|
||||||
|
text = update.message.text
|
||||||
|
|
||||||
|
if text.startswith("/stop"):
|
||||||
|
chat.sendMessage( "Ok, I'll stop sending you messages. If you want to restart just type /start")
|
||||||
|
chat.delete()
|
||||||
|
elif text.startswith("/start"):
|
||||||
|
chat.sendMessage("I already know you, no need to /start me again!")
|
||||||
|
elif text.startswith("/"):
|
||||||
|
chat.sendMessage("Sorry, I don't know that command")
|
||||||
|
|
||||||
|
def sendMessage(self, chat, message):
|
||||||
|
self.bot.sendMessage(chat_id=chat.chat_id, text=message)
|
||||||
|
|
||||||
|
def broadcastMessage(self,message):
|
||||||
|
chats = Chat.objects.all()
|
||||||
|
|
||||||
|
for chat in chats:
|
||||||
|
chat.sendMessage(message)
|
||||||
|
|
||||||
|
|
||||||
|
class Chat(models.Model):
|
||||||
|
chat_id = models.CharField(max_length=255, null=False, blank=False)
|
||||||
|
manager = models.ForeignKey('TelegramManager', related_name='chats', blank=False, null=False)
|
||||||
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=False) #Model must have
|
||||||
|
|
||||||
|
api_key = models.CharField(max_length=255, null=False, blank=False)
|
||||||
|
|
||||||
|
def sendMessage(self,message):
|
||||||
|
self.manager.sendMessage(self,message)
|
||||||
|
|
||||||
|
from django.db.models.signals import pre_save
|
||||||
|
from django.dispatch import receiver
|
||||||
|
from reversion.models import Revision
|
||||||
|
|
||||||
|
@receiver(pre_save, sender=Revision)
|
||||||
|
def my_handler(sender, **kwargs):
|
||||||
|
print "I just got triggered"
|
||||||
13
temp.py
Normal file
13
temp.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from telegram_interface import models
|
||||||
|
|
||||||
|
# man = models.TelegramManager(token="135462350:AAEhnE-Nd90fQIYM988H3rfUKVsT2MI24_A")
|
||||||
|
# man.save()
|
||||||
|
|
||||||
|
man = models.TelegramManager.objects.all()[0]
|
||||||
|
|
||||||
|
|
||||||
|
man.lookupUpdates()
|
||||||
|
|
||||||
|
man.broadcastMessage("THIS IS A BROADCAST, CAN YOU HEAR ME?")
|
||||||
|
|
||||||
|
exit()
|
||||||
Reference in New Issue
Block a user