What'd I Gain?
My girlfriend and I use a group we call Tarejas (Spanish for tasks) for managing all the little things we need to get done throughout the day. Not only does it help keep up focused but there's a sense of achievement finishing all those things in a day and knowing that we've been that much productive. Downside was that we had to keep on typing out the same stuff and editing the list. Soooooooo, we know that I've been wanting to increase that web presence so why not kill two stones with a bird (or however) and make a tool to make my life easier and make you folks reading this think I'm smart for coming up with it! OK! Let's begin!
Notes and Important Links
Wrapper Used: https://github.com/python-telegram-bot/python-telegram-bot (makes it easier to develop for the API with Python)
Example of how a bot in Telegram works: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-Your-first-Bot
On running the program:
- Ensure to use the following command #python [program name] & to make sure the program runs in the background
- You can view all services running with #ps -e
- To terminate the program use #kill [program_ID taken from ps -e]
So to get started you'll need to do a couple things:
- Have a Telegram account. I highly recommend this app and think, minus not having a video calling feature, it beats WhatsApp out in every conceivable way and is very dev-friendly. Link to get telegram: https://telegram.org/
- Contact Telegram's bot-father which you can find at this link: https://telegram.me/botfather
- Follow the setup instructions after contacting the bot (it's really very simple)
- After you're done naming your bot and giving it a unique name make sure you keep the API key handy, you'll need that to speak with your bot
Getting the wrapper
The installation for the wrapper is pretty simple (as are most of the things in this post) ((The following below are commands for the Linux Terminal))
- [yum/apt-get] git
- git clone https://github.com/python-telegram-bot/python-telegram-bot --recursive
- cd python-telegram-bot
- python setup.py install
""" The intention of this program is to provide an easy way for me and my girlfriend, Mikaela, to have a to-do list of items to keep track of and help to increase our productivity. I've made my best attempt to add comments along the code to explain what's going on however if it's still difficult to follow there are additional tutorials linked in the documentation """ import datetime #adds functionality for printing the date from telegram.ext import Updater, CommandHandler updater = Updater(token = 'TOKEN-FROM-BOT-FATHER') dispatcher = updater.dispatcher jordan_tasks =  mikaela_tasks =  def tasks(bot, update, args): #function declaration. takes the bot and update objects as well as the arguments passed with the command today = datetime.date.today() dateList =  dateList.append(today) now = datetime.datetime.now() #creates an instance of the current time if now.hour == 23: #clears the lists at 11pm each day del jordan_tasks[:] del mikaela_tasks[:] if args == 'Jordan' or args == 'jordan': args.pop(0) #remove the first argument as it would not be used past knowing what list of tasks to put it in args.insert(0,'-') args = ' '.join(args) #converts the list of arguments into a single string jordan_tasks.append(args) #adds the arguments (minus the name of the individual) to the list of tasks if args == 'Mikaela' or args == 'mikaela': args.pop(0) args.insert(0,'-') args = ' '.join(args) mikaela_tasks.append(args) taskPrint = "LIST OF TASKS TO-DO FOR "+str(dateList) #the date is put in a string format for it to be used with the other strings easily taskPrint += '\n\nJordan: ' for i in range(len(jordan_tasks)): #runs a loop to gather all the items in the working list taskPrint += '\n' taskPrint += jordan_tasks[i] taskPrint += '\n\nMikaela: ' for i in range(len(mikaela_tasks)): taskPrint += '\n' taskPrint += mikaela_tasks[i] bot.send_message(chat_id=update.message.chat_id, text=taskPrint) #sends all the content in the taskPrint variable to the chat def check_task(bot, update, args): today = datetime.date.today() dateList =  dateList.append(today) index = int(args) #this is the second argument passed to the bot in the command that we're turning into an integer index -= 1 #the integer is decremented as the user would not use the index value but the regular numbered value of the item in the list if args == 'Jordan' or args == 'jordan': jordan_tasks[index] += u' \u2714' #adds the unicode tick to the list item if args == 'Mikaela' or args == 'mikaela': mikaela_tasks[index] += u' \u2714' taskPrint = "LIST OF TASKS TO-DO FOR "+str(dateList) taskPrint += '\n\nJordan: ' for i in range(len(jordan_tasks)): taskPrint += '\n' taskPrint += jordan_tasks[i] taskPrint += '\n\nMikaela: ' for i in range(len(mikaela_tasks)): taskPrint += '\n' taskPrint += mikaela_tasks[i] bot.send_message(chat_id=update.message.chat_id, text='checked :)') bot.send_message(chat_id=update.message.chat_id, text=taskPrint) task_handler = CommandHandler('newtask',tasks,pass_args = True) #creates a new command handler that allows arguments to be passed with the telegram command, arguments are considered all strings after the initial command check_handler = CommandHandler('check',check_task,pass_args = True) dispatcher.add_handler(task_handler) #adds the newly created handler to the dispatcher. this allows it to be used in the running of the bot dispatcher.add_handler(check_handler) updater.start_polling() #updater starts polling messages and input from the user(s) in the chat
Wow, the bot's doing its thing!
That's all folks
Yep, that's it! I've got the comments strewn throughout the program and I'll be updating the blog post with a link to my Github repo for the bot. This piece of code took the better part of an evening and went into the night but it does provide the intended functionality (even if the code might be a bit gross). Thanks a lot for stopping by and giving this a read!