From 173c72257c15791c9608a4be9d0c7f51d8c43fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Tue, 13 Mar 2018 21:52:01 +0100 Subject: [PATCH] Add snippet for flag emoji generation --- Code-snippets.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Code-snippets.md b/Code-snippets.md index ffb60fb..7ec619c 100644 --- a/Code-snippets.md +++ b/Code-snippets.md @@ -27,6 +27,8 @@ It is also a follow-up to the page [Introduction to the API](https://github.com/ * [Keyboard Menus](#keyboard-menus) + [Custom Keyboards](#custom-keyboards) + [Remove a custom keyboard](#remove-a-custom-keyboard) + * [Other useful stuff](#other-useful-stuff) + + [Generate flag emojis from country codes](#generate-flag-emojis-from-country-codes) - [Advanced snippets](#advanced-snippets) + [Restrict access to a handler (decorator)](#restrict-access-to-a-handler-decorator) - [Usage](#usage) @@ -240,6 +242,27 @@ See also: [Build a menu with Buttons](#build-a-menu-with-buttons) >>> bot.send_message(chat_id=chat_id, text="I'm back.", reply_markup=reply_markup) ``` +### Other useful stuff + +#### Generate flag emojis from country codes + +The Unicode flag emoji for any country can by definition be calculated from the countries [2 letter country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). The following snippet only works in Python 3. + +```python +>>> OFFSET = 127462 - ord('A') +>>> +>>> def flag(code): +... code = code.upper() +... return chr(ord(code[0]) + OFFSET) + chr(ord(code[1]) + OFFSET) +... +>>> flag('de') +'πŸ‡©πŸ‡ͺ' +>>> flag('us') +'πŸ‡ΊπŸ‡Έ' +>>> flag('ru') +'πŸ‡·πŸ‡Ί' +>>> +``` ## Advanced snippets