Beispiel #1
0
def welcome_message(bot, update):
  chat_id = get_chat_id(update)
  message_id = get_message_id(update)
  chat_record = TBDB.get_chat_entry(chat_id)

  language = None
  if chat_record is not None:
    language = chat_record["lang"]
  elif update.message is not None and update.message.from_user.language_code is not None:
    # Channel posts do not have a language_code attribute
    logger.debug("Language_code: %s", update.message.from_user.language_code)
    language = update.message.from_user.language_code
  
  message = R.get_string_resource("message_welcome", language)
  message = message.replace("{languages}", "/" + "\n/".join(get_language_list())) #Format them to be a list of commands
  bot.send_message(
    chat_id=chat_id, 
    text=message, 
    reply_to_message_id = message_id, 
    parse_mode = "html",
    is_group = chat_id < 0
  )

  if chat_record is None:
    if language is None:
      language = "en-US"

    if len(language) < 5:
      language = R.iso639_2_to_639_1(language)
    
    logger.debug("No record found for chat {}, creating one with lang {}".format(chat_id, language))
    TBDB.create_default_chat_entry(chat_id, language)
Beispiel #2
0
  def __pre__hook(self, fn, u, c, **kwargs):
    b = c.bot

    m = u.message or u.channel_post
    if not m:
      return

    age = (datetime.utcnow() - m.date.replace(tzinfo=None)).total_seconds()
    if age > config.get_config_prop("app")["antiflood"]["age_threshold"]:
      return

    chat_id = get_chat_id(u)
    antiflood.on_chat_msg_received(chat_id)

    if chat_id in self.floods and self.floods[chat_id] is True:
      return

    if not TBDB.get_chat_entry(chat_id):
      # happens when welcome/joined message is not received
      TBDB.create_default_chat_entry(chat_id, 'en-US')
      
    if chat_id in self.mqbot.active_chats_cache and self.mqbot.active_chats_cache[chat_id] == 0:
      logger.debug("Marking chat {} as active".format(chat_id))
      self.mqbot.active_chats_cache[chat_id] = 1
      TBDB.set_chat_active(chat_id, self.mqbot.active_chats_cache[chat_id])

    return fn(b, u, **kwargs)
Beispiel #3
0
def test_db():
    id = 1234

    TBDB.create_default_chat_entry(id, 'en-US')
    assert TBDB.get_chat_lang(id) == 'en-US'
    assert TBDB.get_chat_active(id) == 1

    TBDB.set_chat_lang(id, 'lang')
    TBDB.set_chat_voice_enabled(id, 2)
    TBDB.set_chat_photos_enabled(id, 1)
    TBDB.set_chat_qr_enabled(id, 1)
    TBDB.set_chat_active(id, 0)
    TBDB.set_chat_ban(id, 1)

    assert TBDB.get_chat_lang(id) == 'lang'
    assert TBDB.get_chat_voice_enabled(id) == 2
    assert TBDB.get_chat_photos_enabled(id) == 1
    assert TBDB.get_chat_qr_enabled(id) == 1
    assert TBDB.get_chat_active(id) == 0
    assert TBDB.get_chat_ban(id) == 1