The Wayback Machine - https://web.archive.org/web/20200918095255/https://github.com/php-telegram-bot/core/issues/694
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: per chat settings in database #694

Open
sharkydog opened this issue Nov 6, 2017 · 5 comments
Open

feature: per chat settings in database #694

sharkydog opened this issue Nov 6, 2017 · 5 comments

Comments

@sharkydog
Copy link

@sharkydog sharkydog commented Nov 6, 2017

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);
@noplanman
Copy link
Member

@noplanman noplanman commented Nov 8, 2017

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.

@sharkydog
Copy link
Author

@sharkydog sharkydog commented Nov 8, 2017

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).

@noplanman
Copy link
Member

@noplanman noplanman commented Apr 12, 2018

@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?

@sharkydog
Copy link
Author

@sharkydog sharkydog commented Apr 13, 2018

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
Copy link
Author

@sharkydog sharkydog commented Apr 13, 2018

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.