-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
142 lines (113 loc) · 4.19 KB
/
bot.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
from telegram import InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
from icalendar import Calendar, Event, vText
import logging
import typing
import json
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
actions = {
'NEXT': '1',
'PREV': '2',
'SEARCH': '3'
}
# ------------------ #
# Here goes bot's #
# Implementation #
# ------------------ #
def get_token():
path = 'token.json'
with open(path, 'r') as jsn:
data = json.load(jsn)
return data['token']
# keyboard = [[InlineKeyboardButton("Previous", callback_data=actions['PREV']),
# InlineKeyboardButton("Search by keyword", callback_data=actions['SEARCH']),
# InlineKeyboardButton("Next", callback_data=actions['NEXT'])]]
def start(bot, update):
update.message.reply_text('Start working...')
help_text = list()
help_text.append("Please, enter the event's info in the following format:")
help_text.append("-----")
help_text.append("<title>")
help_text.append("<description>")
help_text.append("<url>")
help_text.append("<location>")
help_text.append("<YYYY><MM><DD>T<hh><mm><ss> - start date")
help_text.append("<YYYY><MM><DD>T<hh><mm><ss> - end date")
help_text.append("-----")
help_text = '\n'.join(help_text)
update.message.reply_text(help_text)
# def button(bot, update):
# global iterator
#
# query = update.callback_query
#
# # NEXT case
# if query.data == actions['NEXT']:
# bot.edit_message_text(text="Ok, next",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
# bot.send_message(query.message.chat_id, '--------------------------')
# bot.send_message(query.message.chat_id, 'The End. Now send me another keyword')
# reply_markup = InlineKeyboardMarkup(keyboard)
# query.message.reply_text('Choose something', reply_markup=reply_markup)
#
# # SEARCH case
# elif query.data == actions['SEARCH']:
# bot.edit_message_text(text="Ok, send me a keyword",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
#
# # PREV case
# elif query.data == actions['PREV']:
# bot.edit_message_text(text="Ok, prev",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
#
# bot.send_message(query.message.chat_id, 'Move Next please')
#
# reply_markup = InlineKeyboardMarkup(keyboard)
# query.message.reply_text('Choose something', reply_markup=reply_markup)
def send_doc(update):
# chat_id = str(update.message.chat_id)
#
# update.message.reply_text('Title: "{}"'.format(doc['title']))
#
# with open(wcloud_path, 'rb') as f:
# update.message.reply_photo(photo=f)
pass
def rules_fun(bot, update):
query = update.message.text
title, description, url, location, date_start, date_end = query.split('\n')
event = Event()
event['summary'] = title
event['description'] = description
event['url'] = url
event['location'] = vText(location)
event['dtstart'] = date_start
event['dtend'] = date_end
cal = Calendar()
cal.add_component(event)
# print(cal)
f = open('event.ics', 'wb')
f.write(cal.to_ical())
f.close()
with open('event.ics', 'rb') as f:
update.message.reply_document(f)
def help_function(bot, update):
update.message.reply_text('Help!')
def main():
updater = Updater(get_token())
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help_function))
# on non-command messages
dp.add_handler(MessageHandler(Filters.text, rules_fun))
# dp.add_handler(CallbackQueryHandler(button))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
print("-> Hi!")
main()