Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

bot.py 5.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  1. import telegram
  2. from telegram.ext import Updater, InlineQueryHandler, CommandHandler, Defaults, Job, MessageHandler, Filters
  3. import requests # to make requests to external api
  4. import re # regex for pictures of doggos
  5. import logging
  6. from datetime import datetime # use to restart everyday
  7. import sys
  8. from consts import *
  9. reports = []
  10. shags_situation = {
  11. 'גדול': {
  12. 'isRasar':0 # is rasar is a number between 0 and 1, 1- rasar, 0 clean
  13. },
  14. 'קטן':{
  15. 'isRasar':0
  16. }
  17. }
  18. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  19. level=logging.INFO)
  20. logger = logging.getLogger()
  21. logger.setLevel(logging.INFO)
  22. if MODE == "dev":
  23. def run(updater):
  24. updater.start_polling()
  25. elif MODE == "prod":
  26. def run(updater):
  27. PORT = int(os.environ.get("PORT", "8443"))
  28. HEROKU_APP_NAME = os.environ.get("HEROKU_APP_NAME")
  29. updater.start_webhook(listen="0.0.0.0",
  30. port=PORT,
  31. url_path=BOT_TOKEN)
  32. updater.bot.set_webhook("https://{}.herokuapp.com/{}".format(HEROKU_APP_NAME, BOT_TOKEN))
  33. else:
  34. logger.error("No MODE specified!")
  35. sys.exit(1)
  36. def reset_shags():
  37. global shags_situation
  38. shags_situation['גדול']['isRasar'] = 0
  39. shags_situation['קטן']['isRasar'] = 0
  40. # For easy monitoring during beta stage
  41. def log_admin(bot, info):
  42. logger.info(info)
  43. bot.send_message(chat_id=ADMIN_ID, text="LOG: %s"%info)
  44. # Get random dog image for the memes
  45. def get_url():
  46. contents = requests.get('https://random.dog/woof.json').json()
  47. image_url = contents['url']
  48. return image_url
  49. def get_image_url():
  50. allowed_extention = ['jpg', 'jpeg', 'png']
  51. file_extention = ''
  52. while file_extention not in allowed_extention:
  53. url = get_url()
  54. file_extention = re.search("([^.]*)$",url).group(1)
  55. logger.info('Sent image url: ' + url)
  56. return url
  57. def bop(bot, update):
  58. chat_id = update.message.chat_id
  59. url = get_image_url()
  60. bot.send_photo(chat_id=chat_id, photo=url)
  61. # Get update of the rasar situation
  62. def update(bot, update):
  63. global reports
  64. chat_id = update.message.chat_id
  65. for shag in shags_situation:
  66. isRasar = shags_situation[shag]['isRasar']
  67. if isRasar>= 0.5:
  68. situation = 'רס"ר'
  69. chance = isRasar*100
  70. else:
  71. situation = 'נקי'
  72. chance = (1-isRasar)*100
  73. bot.send_message(chat_id=chat_id, text = 'שג ' + shag +' '+ situation+ ' בטוח ב- ' + str(chance) +'%')
  74. reply_markup = telegram.ReplyKeyboardMarkup(KEYBOARD)
  75. bot.send_message(chat_id=chat_id,
  76. text="בחרו מהאפשרויות לדיווח",
  77. reply_markup=reply_markup)
  78. def cancel_report(bot, update):
  79. global reports
  80. chat_id = update.message.chat_id
  81. report = next((report for report in reports if report["chat_id"] == chat_id), None)
  82. if report:
  83. reports.remove(report)
  84. calculate_prob()
  85. bot.send_message(chat_id=chat_id, text='דיווח בוטל')
  86. user_name = str(update.effective_user.full_name)
  87. log_admin(bot, "Cancel report by user: %s" % user_name)
  88. else:
  89. bot.send_message(chat_id=chat_id, text='הדיווח כבר בוטל או שלא נשלח מעולם')
  90. reply_markup = telegram.ReplyKeyboardMarkup(KEYBOARD)
  91. bot.send_message(chat_id=chat_id,
  92. text="בחרו מהאפשרויות לדיווח",
  93. reply_markup=reply_markup)
  94. def calculate_prob():
  95. global reports, shags_situation
  96. reset_shags()
  97. for report in reports:
  98. shag = report['shag']
  99. state = report['state']
  100. if state=='רס"ר':
  101. shags_situation[shag]['isRasar'] +=RASAR_FACTOR*(1-shags_situation[shag]['isRasar'])
  102. else:
  103. shags_situation[shag]['isRasar'] *=CLEAN_FACTOR
  104. def report(bot, update):
  105. global reports
  106. chat_id = update.message.chat_id
  107. shag = update.message.text.split()[1]
  108. state = update.message.text.split()[2]
  109. reports.append({
  110. 'shag': shag,
  111. 'state': state,
  112. 'chat_id': chat_id,
  113. 'time': datetime.now()
  114. })
  115. calculate_prob()
  116. custom_keyboard = [['/cancel_report']]
  117. reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
  118. bot.send_message(chat_id=chat_id, text='תודה שדיווחת!', reply_markup=reply_markup)
  119. user_name = str(update.effective_user.full_name)
  120. log_admin(bot, "Recived report: %s %s by user: %s" % (shag, state, user_name))
  121. def send_to_admin(bot, update):
  122. chat_id = update.message.chat_id
  123. message = update.message.text
  124. if len(message.split()) == 1:
  125. bot.send_message(chat_id=chat_id, text='יש לכתוב את הפקודה ' + message + ' ולאחר מכן טקסט חופשי. \n \n טיפ: נגיעה ארוכה על הפקודה תכתוב אותה מבלי לשלוח.')
  126. else:
  127. bot.send_message(chat_id=chat_id, text='תודה רבה :)')
  128. bot.send_message(chat_id=ADMIN_ID, text=message)
  129. def main():
  130. updater = Updater(BOT_TOKEN)
  131. dp = updater.dispatcher
  132. dp.add_handler(CommandHandler('cancel_report',cancel_report))
  133. dp.add_handler(CommandHandler('bop',bop))
  134. dp.add_handler(CommandHandler('report_bug', send_to_admin))
  135. dp.add_handler(CommandHandler('suggest_feature', send_to_admin))
  136. dp.add_handler(CommandHandler('report',report))
  137. dp.add_handler(MessageHandler(Filters.text, update))
  138. run(updater)
  139. updater.idle()
  140. if __name__ == '__main__':
  141. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...