From 6de4b7b6fff7d87d6b6f6d5303309fb537dbebc6 Mon Sep 17 00:00:00 2001 From: respasha Date: Fri, 22 Jul 2016 22:17:23 +0300 Subject: [PATCH] Start message editing --- README.md | 12 +++++++++++- db.py | 19 +++++++++++++++---- proxy_bot.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6580860..06d63f8 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,18 @@ * [Contact](#contact) ##ChangeLog! +####Version1.1 + * **Major Update**: Start Message editing by `/setstartmessage` + Start message is the fist message user sees, when he/she starts the bot. + * **Bugs in this version**: Hopefully no + ####Version1.0 * **Major Update**: 1. All storing data functionality moved to MongoDB. 2. Added paging to users list and blocked list. 3. Simplified user blocking using inline keyboards. 4. Code refactored and beautified. - * **Bugs in this version** : Hopefully no + * **Bugs in this version**: Hopefully no @@ -46,6 +51,7 @@ * Start bot: `bash launch.sh` ## What's new ??? +* You can now set Start Message the same way you do with the Block Message and Unavailable Message. New commands are `/setstartmessage` and `/viewstartmessage` * Bot sends _usercard_ with each user's message (which increases the number of messages, i know. I hope, I'll get to it later). _Usercard_ shows all info about user. Ream more about blocking and unblocking [here](#blocking-and-unblocking-feature). * Admins can set their status as `/available` or `/unavailable`. This means that when you will not be available bot will notify the user if he/she tries to text you by sending him your unavailable message, just like the way you have a pre-recorded message on answering machines! The bot will however forward you the message. You can set and view your unavailable message by typing `/setunavailablemessage` and `/setunavailablemessage` respectively. * You can now view the list of all users bot has seen! Type `/viewuserslist`. The list will contain basic info about every user and a command to their usercard. @@ -64,6 +70,10 @@ The idea of this bot is pretty simple: you just place the bot between you and th

![Screenshot](http://i.imgur.com/YZoiTjd.png) +#### Setting Start Message +You can set Start Message -- the message, which user sees when he/she starts the bot. +Use `/setstartmessage` command for setting it and `/viewstartmessage` for viewing it. + ### Blocking and Unblocking Feature It was messy and complicated for me in Mr_Gigabyte's implementation, so I've fully rewrited it. diff --git a/db.py b/db.py index 4ef24ec..37eb592 100644 --- a/db.py +++ b/db.py @@ -66,14 +66,15 @@ def __init__(self, coll): self.data = { 'availability': 'available', 'blockmsg': "uh..oh you're blocked", - 'nonavailmsg': "I'm sorry i'm apparently offline" + 'nonavailmsg': "I'm sorry i'm apparently offline", + 'startmsg': " Write me your text and the admin will get in touch with you shortly." } result = self.coll.insert_one(self.data) self.data['_id'] = result.inserted_id @property def availability(self): - return self.data['availability'] + return self.data.get('availability') or '' @availability.setter def availability(self, value): @@ -83,7 +84,7 @@ def availability(self, value): @property def blockmsg(self): - return self.data['blockmsg'] + return self.data.get('blockmsg') or '' @blockmsg.setter def blockmsg(self, value): @@ -92,7 +93,7 @@ def blockmsg(self, value): @property def nonavailmsg(self): - return self.data['nonavailmsg'] + return self.data.get('nonavailmsg') or '' @nonavailmsg.setter def nonavailmsg(self, value): @@ -100,6 +101,16 @@ def nonavailmsg(self, value): self.coll.update_one({'_id': self.data['_id']}, {'$set': self.data}) + @property + def startmsg(self): + return self.data.get('startmsg') + + @startmsg.setter + def startmsg(self, value): + self.data['startmsg'] = value + self.coll.update_one({'_id': self.data['_id']}, {'$set': self.data}) + + __db_client = MongoClient(config.db_auth) __db = __db_client[config.db_name] diff --git a/proxy_bot.py b/proxy_bot.py index c86e76b..6f3a7f3 100644 --- a/proxy_bot.py +++ b/proxy_bot.py @@ -73,7 +73,10 @@ def command_help(message): `6`- /viewblockmessage: to view the block message (that the users will see) `7`- /setblockmessage : set the text message that you want users to see when they are blocked `8`- /viewblocklist : allows you to view the list of blocked users -`9`- /viewuserlist : allows you to view all non-blocked users in database\n +`9`- /viewuserlist : allows you to view all non-blocked users in database +`10`- /setstartmessage : set the text message that you want users to see when they start the bot +`11`- /viewstartmessage : to view the Start Message +\n *For any help and queries please contact -* [me](telegram.me/phash_bot) *or check out* [source code](https://github.com/p-hash/proxybot)""", parse_mode="Markdown" ) @@ -98,7 +101,7 @@ def command_viewblockmessage(message): ) -# command for admin to set the block message that the user after getting blocked +# command for admin to set the block message that the user see after getting blocked @bot.message_handler(func=lambda message: message.chat.id == config.my_id, commands=["setblockmessage"]) def command_setblockmessage(message): blockmsg = bot.send_message( @@ -119,6 +122,46 @@ def save_blockmsg(message): ) +# command for admin: Used to view the start message +@bot.message_handler(func=lambda message: message.chat.id == config.my_id, commands=["viewstartmessage"]) +def command_viewstartmessage(message): + if not db.common.startmsg: + bot.send_message( + message.chat.id, + """*Oops!* +You haven't set any *Start Message* for the users. +To set one kindly send: /setstartmessage to me""", + parse_mode="Markdown" + ) + else: + bot.send_message( + message.chat.id, + "`Your Start Message:`" + "\n" + db.common.startmsg, + parse_mode="Markdown" + ) + + +# command for admin to set the start message that the user see when he starts the bot +@bot.message_handler(func=lambda message: message.chat.id == config.my_id, commands=["setstartmessage"]) +def command_setstartmessage(message): + startmsg = bot.send_message( + message.chat.id, + "Alright now send me your text that you want the user to see when he/she *starts* the bot", + parse_mode="Markdown" + ) + bot.register_next_step_handler(startmsg, save_startmsg) + + +# Next step handler for saving blockmsg +def save_startmsg(message): + db.common.startmsg = str(message.text) + bot.reply_to( + message, + "Thanks! " + "\n" + "*The new Start Message has been set successfully* ", + parse_mode="Markdown" + ) + + # command for admin # view the whole Block List containing usernames and nicknames of the blocked users, refer config.py for more info @bot.message_handler(func=lambda message: message.chat.id == config.my_id, commands=["viewblocklist"]) @@ -268,7 +311,7 @@ def save_nonavailmsg(message): db.common.nonavailmsg = str(message.text) bot.reply_to( message, - "Thanks! " + "\n" + "*The new NonAvailable Message has been set successfully* ", + "Thanks! " + "\n" + "*The new Unavailable Message has been set successfully* ", parse_mode="Markdown" ) @@ -320,8 +363,7 @@ def command_checkstatus(message): def command_start_all(message): bot.send_message( message.chat.id, - "Hey " + message.chat.first_name + "!" + "\n" + - " Write me your text and the admin will get in touch with you shortly." + "Hey " + message.chat.first_name + "!\n" + db.common.startmsg )