Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2020 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of jerrycan. 

5 

6jerrycan is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11jerrycan is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with jerrycan. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20from bokkichat.entities.Address import Address 

21from bokkichat.entities.message.TextMessage import TextMessage 

22from jerrycan.base import db, app 

23from jerrycan.Config import Config 

24from jerrycan.db.IDModelMixin import IDModelMixin 

25from jerrycan.db.User import User 

26 

27 

28class TelegramChatId(IDModelMixin, db.Model): 

29 """ 

30 Model that describes the 'telegram_chat_ids' SQL table 

31 Maps telegram chat ids to users 

32 """ 

33 

34 __tablename__ = "telegram_chat_ids" 

35 """ 

36 The name of the table 

37 """ 

38 

39 user_id: int = db.Column( 

40 db.Integer, db.ForeignKey("users.id"), 

41 nullable=False 

42 ) 

43 """ 

44 The ID of the user associated with this telegram chat ID 

45 """ 

46 

47 user: User = db.relationship("User", back_populates="telegram_chat_id") 

48 """ 

49 The user associated with this telegram chat ID 

50 """ 

51 

52 chat_id: str = db.Column(db.String(255), nullable=False) 

53 """ 

54 The telegram chat ID 

55 """ 

56 

57 def send_message(self, message_text: str): 

58 """ 

59 Sends a message to the telegram chat 

60 :param message_text: The message text to send 

61 :return: None 

62 """ 

63 try: 

64 address = Address(self.chat_id) 

65 message = TextMessage( 

66 Config.TELEGRAM_BOT_CONNECTION.address, 

67 address, 

68 message_text 

69 ) 

70 Config.TELEGRAM_BOT_CONNECTION.send(message) 

71 except AttributeError: 

72 app.logger.error("Failed to send telegram message: no connection")