├── .gitignore ├── requirements.txt ├── README.md ├── main.py └── leave_record.py /.gitignore: -------------------------------------------------------------------------------- 1 | /__pycache__ 2 | /bin 3 | /include 4 | /lib 5 | webhook_pkey.key 6 | webhook_cert.pem 7 | settings.ini 8 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asn1crypto==0.24.0 2 | certifi==2019.6.16 3 | cffi==1.12.3 4 | chardet==3.0.4 5 | cryptography==2.7 6 | future==0.17.1 7 | graphqlclient==0.2.4 8 | idna==2.8 9 | pycparser==2.19 10 | python-decouple==3.1 11 | python-telegram-bot==12.0.0b1 12 | requests==2.22.0 13 | six==1.12.0 14 | tornado==6.0.2 15 | urllib3==1.25.3 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bot 2 | 3 | [![Join the chat at https://gitter.im/amfoss/bot](https://badges.gitter.im/amfoss/bot.svg)](https://gitter.im/amfoss/bot?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | Telegram bot to manage club activities 6 | 7 | ## Features 8 | * **Recording Leave Application**: Collects leave records from members and logs it into CMS 9 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3.6 2 | import logging 3 | import os 4 | from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler, 5 | ConversationHandler) 6 | from decouple import config 7 | 8 | 9 | from leave_record import LeaveRecord 10 | 11 | # Enable logging 12 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 13 | level=logging.INFO) 14 | 15 | logger = logging.getLogger(__name__) 16 | 17 | TYPE, REASON = range(2) 18 | 19 | 20 | def start(bot, context): 21 | user = bot.message.from_user 22 | bot.message.reply_text("Hi @" + user['username'] + "!\n I am chowkidar of amFOSS.") 23 | bot.message.reply_text("/help - for more details") 24 | 25 | def help(bot, context): 26 | bot.message.reply_text( 27 | "This bot will help you with all the functionalities.\n\n" 28 | "/leaverecord - register your leave record" 29 | ) 30 | 31 | def error(update, context): 32 | logger.warning('Update "%s" caused error "%s"', update, error) 33 | 34 | 35 | def main(): 36 | TOKEN = config('BOT_TOKEN') 37 | updater = Updater(TOKEN, use_context=True) 38 | dp = updater.dispatcher 39 | dp.add_handler(CommandHandler("start", start)) 40 | dp.add_handler(CommandHandler("help", help)) 41 | 42 | l = LeaveRecord 43 | leave_handler = ConversationHandler( 44 | entry_points=[CommandHandler('leaverecord', l.getType)], 45 | 46 | states={ 47 | TYPE: [MessageHandler(Filters.regex('^(Health|Family/Home|Tired|Academics|Duty)$'), l.getReason)], 48 | REASON: [MessageHandler(Filters.text, l.registerLeave)] 49 | }, 50 | 51 | fallbacks=[CommandHandler('cancel', l.cancel)] 52 | ) 53 | 54 | dp.add_handler(leave_handler) 55 | dp.add_error_handler(error) 56 | HOST = config('HOST') 57 | PORT = config('PORT') 58 | KEY = config('KEY') 59 | CERT = config('CERT') 60 | updater.start_webhook(listen='0.0.0.0', 61 | port=PORT, 62 | url_path=TOKEN, 63 | key=KEY, 64 | cert=CERT, 65 | webhook_url=HOST+':'+PORT+'/'+TOKEN) 66 | # updater.start_polling() 67 | updater.idle() 68 | 69 | 70 | if __name__ == '__main__': 71 | main() 72 | -------------------------------------------------------------------------------- /leave_record.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3.6 2 | from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove 3 | from telegram.ext import ConversationHandler 4 | from graphqlclient import GraphQLClient 5 | import json 6 | from decouple import config 7 | 8 | TYPE, REASON = range(2) 9 | 10 | def get_cms_token(): 11 | api_url = config('API_URL') 12 | client = GraphQLClient(api_url) 13 | query = """ 14 | mutation TokenAuth($username: String!, $password: String!) { 15 | tokenAuth(username: $username, password: $password) { 16 | token 17 | } 18 | } 19 | """ 20 | admin_user = config('ADMIN_USER') 21 | admin_pass = config('ADMIN_PASS') 22 | variables = { 23 | "username":admin_user, 24 | "password": admin_pass 25 | } 26 | result = json.loads(client.execute(query, variables)) 27 | return result["data"]["tokenAuth"]["token"] 28 | 29 | def getType(t): 30 | if t == "Health": 31 | return "M" 32 | elif t == "Family/Home": 33 | return "F" 34 | elif t == "Academics": 35 | return "A" 36 | return "T" 37 | 38 | def mutate_cms(d): 39 | api_url = config('API_URL') 40 | client = GraphQLClient(api_url) 41 | query =""" 42 | mutation RecordLeaveToday($userId: String!, $reason: String!, $type: String!, $botToken: String!, $token: String!) 43 | { 44 | RecordLeaveToday(userId: $userId, reason: $reason, type: $type, botToken: $botToken, token: $token) 45 | { 46 | id 47 | } 48 | } 49 | """ 50 | token = config('BOT_TOKEN') 51 | variables = { 52 | "userId": d['user'], 53 | "reason": d['reason'], 54 | "type": getType(d['type']), 55 | "botToken": token, 56 | "token": get_cms_token() 57 | } 58 | print(variables) 59 | client.execute(query, variables) 60 | 61 | class LeaveRecord: 62 | 63 | def getType(bot, context): 64 | reply_keyboard = [['Health', 'Family/Home', 'Tired', "Academics"]] 65 | 66 | bot.message.reply_text("Sad to know that, you wont be coming to lab today...\n" 67 | "What's the general cause?", 68 | reply_markup=ReplyKeyboardMarkup(reply_keyboard, 69 | one_time_keyboard=True)) 70 | return TYPE 71 | 72 | def getReason(bot, context): 73 | d = context.user_data 74 | d['type'] = bot.message.text 75 | bot.message.reply_text('Hmmm... Can you tell a little more about it?', reply_markup=ReplyKeyboardRemove()) 76 | 77 | return REASON 78 | 79 | 80 | def registerLeave(bot, context): 81 | d = context.user_data 82 | d['user'] = bot.message.from_user['id'] 83 | d['reason'] = bot.message.text 84 | bot.message.reply_text("Thank you for informing me.") 85 | bot.message.reply_text("Hope to see you soon again in the lab...") 86 | mutate_cms(d) 87 | 88 | return ConversationHandler.END 89 | 90 | def cancel(bot, context): 91 | bot.message.reply_text('Bye! I hope we can talk again some day.', 92 | reply_markup=ReplyKeyboardRemove()) 93 | 94 | return ConversationHandler.END 95 | --------------------------------------------------------------------------------