-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmate-bot.py
More file actions
71 lines (59 loc) · 2.24 KB
/
mate-bot.py
File metadata and controls
71 lines (59 loc) · 2.24 KB
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
#/usr/bin/python
import sys
import json
from telegram.ext import Updater
import threading
import logging
logging.basicConfig(format='%(asctime)s - %(name)s -%(levelname)s - %(message)s',level=logging.INFO)
updater = Updater(token='732629412:AAGozAsOtKqhdK5nTs7_9GKv6rQ-0L754os')
from telegram.ext import CommandHandler
count = {"total": 0}
dispatcher = updater.dispatcher
def start(bot, update):
try:
with open('data.json','r') as fp:
loadedCount = json.load(fp)
global count
count = loadedCount
print count
except IOError:
print "json.data wasnt found"
bot.send_message(chat_id=update.message.chat_id, text="I will now start to count")
def shutdown():
updater.stop()
updater.is_idle = False
def stop(bot, update):
jsonarray = json.dumps(count)
print jsonarray
with open('data.json', 'w') as outfile:
json.dump(count, outfile)
threading.Thread(target=shutdown).start()
def printOutMate(bot, update):
user = update.message.from_user
if str(user.id) in count:
content = user.first_name + " got " + str(count[str(user.id)]) + " mate already."
bot.send_message(chat_id=update.message.chat_id, text=content)
else:
bot.send_message(chat_id=update.message.chat_id, text="Drink more mate!")
def add(bot, update, args):
print count
user = update.message.from_user
userId = str(user.id)
if userId in count:
print "old"
count[userId] = int(count[userId]) + int(args[0])
else:
print "new"
count[userId] = args[0]
count["total"] = int(count["total"]) + int(args[0])
bot.send_message(chat_id=update.message.chat_id, text=("added " + args[0]) + " Mate to " + user.first_name)
#Add all handler to the dispatcher
print_handler = CommandHandler("mate", printOutMate)
start_handler = CommandHandler('start', start)
stop_handler = CommandHandler('stop', stop)
add_handler = CommandHandler('add', add, pass_args=True)
dispatcher.add_handler(print_handler)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(stop_handler)
dispatcher.add_handler(add_handler)
updater.start_polling()