Skip to content

Instantly share code, notes, and snippets.

@masious
Created March 28, 2016 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masious/5c4884142348baaf8f29 to your computer and use it in GitHub Desktop.
Save masious/5c4884142348baaf8f29 to your computer and use it in GitHub Desktop.
@coroutine
def receive_messages():
try:
while not QUIT:
msg = (yield)
if getattr(msg, 'sender', None) == None:
# print("\n\n\nERROR 1\n\n")
continue
if getattr(msg.sender, 'type', None) == None:
# print("\n\n\nERROR 2\n\n")
continue
if msg.sender.type != 'channel':
# print("\n\n\nERROR 3\n\n")
continue
expected = redis_db.get('id_%s_ad' % msg.sender.id)
if expected == None:
# print(msg.sender.id)
# print("\n\n\nERROR 4\n\n")
continue
# Check previous message
from groups.models import Channel
fwd_dt = redis_db.get('channel_%s_start_ading' % msg.sender.id)
if fwd_dt != None:
channel = Channel.objects.get(unique_id=msg.sender.id)
fwd_date = int(fwd_dt.decode('utf-8'))
points = channel.calculate(fwd_date)
channel.points += points
channel.save()
text = '%d امتیاز، به امتیاز کانال «%s» اضافه شد. اطلاعات کانال:' % (points, channel.name)
channel.notify_admin(text, True)
redis_db.delete('id_%s_ad' % channel.pk)
redis_db.delete('channel_%s_start_ading' % msg.sender.id)
continue
# Check current PM
if getattr(msg, 'text', None) != None and expected.decode('utf-8') == msg.text:
to_admin = ('امتیاز گیری، هم‌اکنون شروع شد.\n'
'توجه داشته‌باشید که قبل از حذف لیست، حتما باید پیام دیگری در کانال ارسال کنید '
'تا امتیاز و جایگاه شما، محاسبه و بروزرسانی شود.\n')
channel = Channel.objects.get(unique_id=msg.sender.id)
redis_db.set('channel_%s_start_ading' % channel.unique_id, msg.fwd_date)
channel.notify_admin(to_admin, True)
continue
except GeneratorExit:
pass
except KeyboardInterrupt:
pass
else:
pass
def receive_starter():
receiver.start()
receiver.message(receive_messages())
receiver.stop()
import threading
thread = threading.Thread(target=receive_starter)
thread.daemon = True
thread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment