Skip to content

Commit

Permalink
Support placeholder in welcome message
Browse files Browse the repository at this point in the history
* Fix in some case, bot will delete non bot command message
* Fix if bot has been kicked, group cache process will crash
* Update library support
  • Loading branch information
Too-Naive committed Dec 9, 2017
1 parent fe09315 commit 8e12b91
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ Command | Parameter(s) | Description
`/ping` | N/A | Return current session information
`/poem` | N/A | Read poetry (TBD)

### Placeholder

Bot is now support placeholder, using them in welcome message,
bot will auto-replace them to what you wish

Placeholder | Replace to
---|----
`$name` | new user first name

### Flags detail

Flag | Description | Default status
Expand All @@ -24,13 +33,13 @@ noblue | While bot is admin, it will delete bot command after 5 seconds (need de

### Example

Set welcome message to **Welcome to my group**:
Set welcome message to **Welcome [who] to my group**:
```
/setwelcome **Welcome to my group**
/setwelcome **Welcome $name to my group**
```
or
```
/setwelcome https://gist.githubusercontent.com/anonymous/a196757970b0358db6e69f4cc8413ce5/raw/1a548bf3c2d1c7d38cd1fde70c6a612b5d58f524/welcomemsg
/setwelcome https://gist.githubusercontent.com/anonymous/6446fbae52916bc7fb092dd1ee3f8483/raw/4ad5231d5e2a68458e117db9bed97407dfe6f47b/welcomemsg
```

_After you run this command, bot will reply your a message_
Expand Down
3 changes: 3 additions & 0 deletions botlib/groupcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def add(self,x,need_check_admin=True,not_found=False):
if not not_found:
self.__db_del(x[0])
Log.info('Delete kicked chat:{}',x[0])
return
except telepot.exception.TelegramError as e:
if e[0] == 'Bad Request: chat not found':
if not not_found:
Expand All @@ -52,6 +53,8 @@ def add(self,x,need_check_admin=True,not_found=False):
Log.error('Delete kicked chat:{}', x[0])
else:
raise e
finally:
result = 0
else:
result = 0
self.g[x[0]]={'msg' : x[1],
Expand Down
11 changes: 7 additions & 4 deletions botlib/tgbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def onMessage(self,msg):
Log.debug(3,'Incoming message')
content_type, chat_type, chat_id = self.glance(msg)
Log.debug(3, '[msg = {}]', msg)
# Added process
if content_type == 'new_chat_member' and msg['new_chat_participant']['id'] == self.getid():
self.gcache.add((chat_id,None,0,1,0))
with MainDatabase() as db:
Expand All @@ -102,13 +103,15 @@ def onMessage(self,msg):
self.sendMessage(chat_id,'Please using /setwelcome to setting welcome message',
reply_to_message_id=msg['message_id'])
return
# kicked process
if content_type == 'left_chat_member' and msg['left_chat_member']['id'] == self.getid():
self.gcache.delete(chat_id)
return
# Main process
if msg['chat']['type'] in group_type:
if content_type == 'text' and 'entities' in msg and msg[
'entities'][0]['type'] == 'bot_command': #msg['text'][0] =='/':

'entities'][0]['type'] == 'bot_command' and msg[
'text'][0] == '/': # Prevent suchas './sudo'
get_result = self.gcache.get(chat_id)
if get_result['noblue']:
delete_target_message(self.bot,chat_id,msg['message_id']).start()
Expand Down Expand Up @@ -201,5 +204,5 @@ def onMessage(self,msg):
elif content_type in content_type_concerned:
result = self.gcache.get(chat_id)['msg']
if result:
self.sendMessage(chat_id,b64decode(result),parse_mode='Markdown',
disable_web_page_preview=True,reply_to_message_id=msg['message_id'])
self.sendMessage(chat_id,b64decode(result).replace('$name',msg['new_chat_participant']['first_name']),
parse_mode='Markdown',disable_web_page_preview=True,reply_to_message_id=msg['message_id'])
2 changes: 1 addition & 1 deletion libpy
Submodule libpy updated from 51ddf6 to 56d9e3
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import libpy.BackupSQL as BackupSQL

def main():
global bot,group_cache,poem_cache
Log.info('Strat initializing....')
Log.info('Debug enable: {}',Log.get_debug_info()[0])
Log.debug(1,'Debug level: {}',Log.get_debug_info()[1])
bot = bot_class()
bot_class()
Log.info('Bot is now running!')
Log.info('Starting BackupSQL daemon')
BackupSQL.sql_backup_daemon().start()
Expand Down

0 comments on commit 8e12b91

Please sign in to comment.