You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
10 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"import json\n",
"\n",
"from telegram import ReplyKeyboardMarkup, Update\n",
"from telegram.ext import (\n",
" Updater,\n",
" CommandHandler,\n",
" MessageHandler,\n",
" Filters,\n",
" ConversationHandler,\n",
" PicklePersistence,\n",
" CallbackContext,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# Enable logging\n",
"logging.basicConfig(\n",
" format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO\n",
")\n",
"\n",
"logger = logging.getLogger(__name__)\n",
"\n",
"\n",
"CHOOSING, TYPING_REPLY, TYPING_CHOICE = range(3)\n",
"\n",
"#Create bot keyboard\n",
"reply_keyboard = [\n",
" ['/start','I would Submit a Translation'],\n",
" ['Want an Explaination', 'Go to the Project'],\n",
" ['Done'],\n",
"]\n",
"\n",
"markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def facts_to_str(user_data):\n",
" facts = list()\n",
"\n",
" for key, value in user_data.items():\n",
" facts.append('{}'.format(value))\n",
"\n",
" return \"\\n\".join(facts).join(['\\n', '\\n'])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# the bot starts here\n",
"\n",
"def start(update: Update, context: CallbackContext) -> None:\n",
" first_name = update.message.from_user['first_name']\n",
" reply_text = f\"Hi, {first_name}! This is Practical Vision Bot.\"\n",
" update.message.reply_text(reply_text, reply_markup=markup)\n",
"\n",
" return CHOOSING"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"#function for submitting\n",
"\n",
"def submit(update: Update, context: CallbackContext) -> None:\n",
" text = update.message.text.lower()\n",
" context.user_data['choice'] = text\n",
" #if context.user_data.get(text):\n",
" # reply_text = 'Your {}, I already know the following ' 'about that: {}'.format(\n",
" # text, context.user_data[text]\n",
" # )\n",
" #else:\n",
" reply_text = f'Super! Please, respect the following format: \\n\\n english, translation, which-language \\n\\n For Example: \\n hello, ciao, italian \\n\\n I would suggest you to translate `Practical Vision` :) !'.format(text)\n",
" update.message.reply_text(reply_text)\n",
"\n",
" return TYPING_REPLY"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"#function for explaination\n",
"\n",
"def exp(update: Update, context: CallbackContext) -> None:\n",
" first_name = update.message.from_user['first_name']\n",
" update.message.reply_text(f\"`PRACTICAL VISION: the fresh opportunity for disseminating African literature that the digital age makes possible.` (Ngũgi wa Thiongo) \\n\\n The future should be multi-lingual: this Telegram BOT makes a crowd-sourced dictionary with your translations. \\n You can add every word/sentence you want from english to be translated into any language! \\n Soon you will be able to have a look to the dictionary.\", reply_markup= markup) "
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"#function for link proj\n",
"\n",
"def project(update: Update, context: CallbackContext) -> None:\n",
" update.message.reply_text(f\"Here it is! \\n \\n https://issue.xpub.nl/13/PRACTICAL_VISION/printing \\n Made with love from Federico Poni \\n in the context of https://issue.xpub.nl/13 \\n\\n Made with love from XPUB1\", reply_markup= markup)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"#function for echo submission\n",
"\n",
"def received_information(update: Update, context: CallbackContext) -> None:\n",
" first_name = update.message.from_user['first_name']\n",
" text = update.message.text\n",
" category = context.user_data['choice']\n",
" context.user_data[category] = text.lower()\n",
" del context.user_data['choice']\n",
" prova = context.user_data[category]\n",
" \n",
" if ',' not in prova:\n",
" update.message.reply_text(f\"Hey, it was wrong\".format(facts_to_str(context.user_data)),\n",
" reply_markup=markup,\n",
" )\n",
" return CHOOSING\n",
" \n",
" else:\n",
" appending = prova.replace(' ','').split(',')\n",
" \n",
" \n",
" \n",
" f = open('PracticalDictionary.json',)\n",
"\n",
" data = json.load(f)\n",
"\n",
" if appending[0] not in data:\n",
" data[appending[0]] = {appending[2]:appending[1]}\n",
" elif appending[0] in data:\n",
" data[appending[0]][appending[2]] = appending[1]\n",
"\n",
" dictionary = data\n",
"\n",
" with open(\"PracticalDictionary.json\", \"w\") as outfile: \n",
" json.dump(dictionary, outfile) \n",
"\n",
" \n",
" update.message.reply_text(f\"Cool {first_name}!\" \" You have just submitted\" \"{}\" \"You can submit more translations, if you want :)\".format(facts_to_str(context.user_data)),\n",
" reply_markup=markup,\n",
" )\n",
"\n",
" return CHOOSING"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"#function for echo everything submitted\n",
"\n",
"def show_data(update: Update, context: CallbackContext) -> None:\n",
" update.message.reply_text(\n",
" \"This is what you already told me:\" \"{}\".format(facts_to_str(context.user_data))\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"#function for end bot and echo everything submitted\n",
"\n",
"def done(update: Update, context: CallbackContext) -> None:\n",
" update.message.reply_text(\n",
" \"This is what you submitted today:\" \n",
" \"{}\".format(facts_to_str(context.user_data)),reply_markup=markup,\n",
" )\n",
" return ConversationHandler.END\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2020-12-07 12:36:35,085 - apscheduler.scheduler - INFO - Scheduler started\n"
]
}
],
"source": [
"def main():\n",
" # Create the Updater and pass it your bot's token.\n",
" pp = PicklePersistence(filename='conversationbot')\n",
" updater = Updater(\"1452534708:AAERTrbN_LUDSLkf0J8sJjvMEEuwfRNka50\", persistence=pp, use_context=True)\n",
"\n",
" # Get the dispatcher to register handlers\n",
" dp = updater.dispatcher\n",
"\n",
" # Add conversation handler with the states CHOOSING, TYPING_CHOICE and TYPING_REPLY\n",
" conv_handler = ConversationHandler(\n",
" entry_points=[CommandHandler('start', start)],\n",
" states={\n",
" CHOOSING: [\n",
" MessageHandler(Filters.regex('^Want an Explaination$'), exp),\n",
" MessageHandler(Filters.regex('^Go to the Project$'), project),\n",
" MessageHandler(Filters.regex('^I would Submit a Translation$'), submit)\n",
" ],\n",
" TYPING_CHOICE: [\n",
" MessageHandler( Filters.text & ~(Filters.command | Filters.regex('^Done$')), start)\n",
" ],\n",
" TYPING_REPLY: [\n",
" MessageHandler(Filters.text & ~(Filters.command | Filters.regex('^Done$')), received_information)\n",
" ]\n",
" },\n",
" fallbacks=[MessageHandler(Filters.regex('^Done$'), done)],\n",
" name=\"my_conversation\",\n",
" persistent=True,\n",
" )\n",
" \n",
" dp.add_handler(conv_handler)\n",
"\n",
" show_data_handler = CommandHandler('show_data', show_data)\n",
" dp.add_handler(show_data_handler)\n",
" \n",
" # Start the Bot\n",
" updater.start_polling()\n",
"\n",
" # Run the bot until you press Ctrl-C or the process receives SIGINT,\n",
" # SIGTERM or SIGABRT. This should be used most of the time, since\n",
" # start_polling() is non-blocking and will stop the bot gracefully.\n",
" updater.idle()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}