-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollbot.py
170 lines (125 loc) · 4.44 KB
/
pollbot.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import logging, re, datetime, time
import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Location
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
bot_token = 'BOTTOKEN'
chat_id = 'CHATID'
pollreset = datetime.time(hour=22, minute=30)
polltime = datetime.time(hour=19, minute=30)
daytorunpoll = 6 # 0:Monday, 1:Tuesday, ....
daytoresetpoll = 3
text1 = 'first answer'
text2 = 'second answer'
day = 3 # day for the event || 0:Monday, 1:Tuesday, ....
# Poll Message in Poll function
textuser1 = 'positiv'
textuser2 = 'negative'
textuserended1 = 'positiv'
textuserended2 = 'negative'
pollendmessage = 'poll ended!'
#If you want to send a location comment out the line with the comment location
#loc = Location(0.0, 0.0)
bot = telegram.Bot(token=bot_token)
polluser = []
query = False
keyboard = [[InlineKeyboardButton(text1, callback_data='1')], [
InlineKeyboardButton(text2, callback_data='2')]]
keyboard_reset = []
reply_markup = InlineKeyboardMarkup(keyboard)
reset_markup = InlineKeyboardMarkup(keyboard_reset)
TYPING_CHOICE = range(1)
def next_weekday(d, weekday):
days_ahead = weekday - d.weekday()
if days_ahead < 0: # Target day already happened this week
days_ahead += 7
return d + datetime.timedelta(days_ahead)
def next_day():
d = datetime.date(time.localtime()[0], time.localtime()[1], time.localtime()[2])
next_day = next_weekday(d, day)
next_day = str(next_day.strftime("%d.%m.%Y"))
return next_day
pollmessage = ''
def error(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
def poll():
global pollmessage
pollmessage = next_day() + 'MESSAGE' #Message next_day() calculate the next date of the defined day
# bot.sendLocation(chat_id=chat_id, location=loc) #location
message = bot.send_message(chat_id=chat_id, text=pollmessage, reply_markup=reply_markup)
def pollen(update):
poll()
def pollen_by_command(update, context):
poll()
def button(update,context):
global query
query = update.callback_query
# addtext = ""
us = ""
id = str(query.from_user['id'])
des = str(query.data)
if des == "0":
bot.send_message(chat_id=chat_id, text='Poll over!')
return
if query.from_user["username"]:
us = '@' + str(query.from_user["username"])
elif query.from_user["first_name"]:
us = str(query.from_user["first_name"])
else:
us = str(query.from_user["last_name"])
if [id, des, us] in polluser:
return
else:
if [id, "2", us] in polluser:
polluser[polluser.index([id, "2", us])][1] = "1"
elif [id, "1", us] in polluser:
polluser[polluser.index([id, "1", us])][1] = "2"
else:
polluser.append([id, des, us])
query.edit_message_text(text = pollmessage + combineText(polluser), reply_markup = reply_markup)
def combineText(arr):
text = ''
for x in arr:
des = x[1]
us = x[2]
if des == "1":
text = text + " \n" + us + textuser1
elif des == "2":
text = text + " \n" + us + textuser2
return text
def combineText_past(arr):
text = ''
for x in arr:
des = x[1]
us = x[2]
if des == "1":
text = text + " \n" + us + textuserended1
elif des == "2":
text = text + " \n" + us + textuserended2
return text
def reset():
global query
global polluser
if query:
query.edit_message_text(text= pollendmessage + '\n' + combineText_past(polluser), reply_markup=reset_markup)
query = False
polluser = []
def reset_call(update):
reset()
def reset_by_command(update, context):
reset()
if __name__ == '__main__':
updater = Updater(bot_token, use_context=True)
jobQ = updater.job_queue
dp = updater.dispatcher
jobQ.run_daily(pollen, polltime, days=(daytorunpoll, ))
jobQ.run_daily(reset_call, pollreset, days=(daytoresetpoll, ))
dp.add_handler(CommandHandler("pollen", pollen_by_command))
dp.add_handler(CommandHandler("reset", reset_by_command))
dp.add_handler(CallbackQueryHandler(button))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()