core icon indicating copy to clipboard operation
core copied to clipboard

feature: per chat settings in database

Open sharkydog opened this issue 6 years ago • 5 comments

Currently, to avoid using another database or flat files I am doing this:

$settings = new Conversation(
	$this->telegram->getBotId(),
	$this->getMessage()->getChat()->getId(),
	'settings'
);
$settings->notes['key'] = 'value';
$settings->update();

Will be nice to have something in core to be used like this:

$chat = $this->getMessage()->getChat()->getId();
$value = $this->settingsGet('key', $chat);
$this->settingsSet('key', $value, $chat);

sharkydog avatar Nov 06 '17 13:11 sharkydog

There was a discussion about this here: #441 Hasn't been fully discussed or implemented yet.

Feel free to suggest a PR for this 👍

Would be cool to have various types of settings, possibly also bot-wide ones, to allow custom changes to admin IDs etc.

noplanman avatar Nov 08 '17 12:11 noplanman

And then having two or more arguments to consider (command, chat, user) comes the headache of what has priority over what. It actually make more sense to have command settings with global fallback than what I need (chat settings).

sharkydog avatar Nov 08 '17 17:11 sharkydog

@sharkydog I've added a very simple option saving mechanism in the support bot here: https://github.com/php-telegram-bot/support-bot/blob/481eae52f8cc986bd724a4a53ea9d6895bf08c69/public/commands/NewchatmembersCommand.php#L190

I want to add this to core and was wondering if you have any ideas on how best to implement this so that it could allow per-chat settings. Maybe add an extra column to the database table that denotes which chat it comes from if the setting was set specifically within a chat command?

noplanman avatar Apr 12 '18 20:04 noplanman

Or Yet Another Maybe - to avoid having multiple key columns and arguments to the functions: Try to get "key.$command.$chat", if not found try "key.$command", if not "key". This can be written from simple php loop to single sql query to return the first key available. And the order is as desired by the user, if you know you haven't set any user, command, chat or other overrides, you simply get "key", or you can try "key.$chat.$user.$command" and still get "key". With func_get_args(), key parts can be turned to arguments and joined with dot.

sharkydog avatar Apr 13 '18 07:04 sharkydog

maybe also offer options only through func_get_args() so you do not expose the underlying format, like:

function optionGet() {
	$args = func_get_args(); // sql clean/escape this
	$where = [];
	
	while(!empty($args)) {
		$where[] = "`option`='".implode('.',$args)."'";
		array_pop($args);
	}
	
	$sql = "SELECT `val` FROM `options`";
	$sql .= " WHERE ".implode(" OR ",$where);
	$sql .= " ORDER BY `option` DESC LIMIT 1";
	
	// execute and return only column or null
}

sharkydog avatar Apr 13 '18 08:04 sharkydog