Nareshkumar Rao
3 years ago
committed by
GitHub
8 changed files with 212 additions and 295 deletions
@ -1,80 +1,60 @@ |
|||||
import { TelegramID, UserRowID, VerificationString } from "../../types"; |
import { TelegramID, UserRowID, VerificationString } from "../../types"; |
||||
import { User, UserInstance } from "./User"; |
import { User, UserInstance } from "./User"; |
||||
|
|
||||
export function getUserByTelegramID( |
|
||||
telegramID: TelegramID, |
|
||||
callback: (user?: UserInstance, message?: string) => void |
|
||||
): void { |
|
||||
User.findOne({ |
|
||||
|
export async function getUserByTelegramID( |
||||
|
telegramID: TelegramID |
||||
|
): Promise<UserInstance | null> { |
||||
|
const user = await User.findOne({ |
||||
where: { |
where: { |
||||
telegram: telegramID, |
telegram: telegramID, |
||||
}, |
}, |
||||
}) |
|
||||
.then((result) => { |
|
||||
callback(!!result ? result : undefined); |
|
||||
}) |
|
||||
.catch(() => { |
|
||||
callback(undefined); |
|
||||
}); |
}); |
||||
|
return user; |
||||
} |
} |
||||
|
|
||||
export function getUserByRowID( |
|
||||
rowID: UserRowID, |
|
||||
callback: (user?: UserInstance, message?: string) => void |
|
||||
): void { |
|
||||
User.findOne({ |
|
||||
|
export async function getUserByRowID( |
||||
|
rowID: UserRowID |
||||
|
): Promise<UserInstance | null> { |
||||
|
const user = await User.findOne({ |
||||
where: { |
where: { |
||||
id: rowID, |
id: rowID, |
||||
}, |
}, |
||||
}) |
|
||||
.then((result) => { |
|
||||
callback(!!result ? result : undefined); |
|
||||
}) |
|
||||
.catch(() => { |
|
||||
callback(undefined); |
|
||||
}); |
}); |
||||
|
return user; |
||||
} |
} |
||||
|
|
||||
export function getUserByVerification( |
|
||||
verification: VerificationString, |
|
||||
callback: (user?: UserInstance, message?: string) => void |
|
||||
): void { |
|
||||
User.findOne({ |
|
||||
|
export async function getUserByVerification( |
||||
|
verification: VerificationString |
||||
|
): Promise<UserInstance | null> { |
||||
|
const user = await User.findOne({ |
||||
where: { |
where: { |
||||
verification: verification, |
verification: verification, |
||||
}, |
}, |
||||
}) |
|
||||
.then((result) => { |
|
||||
callback(!!result ? result : undefined); |
|
||||
}) |
|
||||
.catch(() => { |
|
||||
callback(undefined); |
|
||||
}); |
}); |
||||
|
return user; |
||||
} |
} |
||||
|
|
||||
export function getUserCovidPositivity(telegramID: TelegramID, callback: (isInfected?: boolean) => void): void { |
|
||||
getUserByTelegramID(telegramID, user => { |
|
||||
if (!!user) { |
|
||||
const infectionDuration = Date.now() - +user.infectionDate; |
|
||||
|
export async function getUserCovidPositivity( |
||||
|
telegramID: TelegramID |
||||
|
): Promise<boolean> { |
||||
|
const user = await getUserByTelegramID(telegramID); |
||||
|
if (!user) throw new Error("User not found"); |
||||
|
const infectionDuration = new Date().getTime() - user.infectionDate.getTime(); |
||||
if (infectionDuration > 60 * 60 * 24 * 14 * 1000) { |
if (infectionDuration > 60 * 60 * 24 * 14 * 1000) { |
||||
setUserCovidPositivity(telegramID, false, success => { |
|
||||
callback(success ? false : undefined); |
|
||||
}); |
|
||||
} else { |
|
||||
callback(user.isInfected); |
|
||||
} |
|
||||
|
await setUserCovidPositivity(telegramID, false); |
||||
|
return false; |
||||
} else { |
} else { |
||||
callback(); |
|
||||
|
return user.isInfected; |
||||
} |
} |
||||
}); |
|
||||
} |
} |
||||
|
|
||||
export function setUserCovidPositivity(telegramID: TelegramID, infectionState: boolean, callback: (success: boolean) => void): void { |
|
||||
getUserByTelegramID(telegramID, user => { |
|
||||
if (!!user) { |
|
||||
|
export async function setUserCovidPositivity( |
||||
|
telegramID: TelegramID, |
||||
|
infectionState: boolean |
||||
|
): Promise<void> { |
||||
|
const user = await getUserByTelegramID(telegramID); |
||||
|
if (!user) throw new Error("User not found"); |
||||
user.isInfected = infectionState; |
user.isInfected = infectionState; |
||||
user.infectionDate = new Date(); |
user.infectionDate = new Date(); |
||||
user.save().then(() => callback(true)).catch(() => callback(false)); |
|
||||
} else { callback(false) } |
|
||||
}); |
|
||||
|
if (!(await user.save())) throw new Error("Could not save user state"); |
||||
} |
} |
||||
|
@ -1,56 +1,30 @@ |
|||||
import { strings_en } from "../strings"; |
import { strings_en } from "../strings"; |
||||
import { sendTelegramMessage } from "../telegram"; |
import { sendTelegramMessage } from "../telegram"; |
||||
import { TelegramID, UserRowID } from "../types"; |
|
||||
|
import { TelegramID } from "../types"; |
||||
import { Contact } from "./models/Contact"; |
import { Contact } from "./models/Contact"; |
||||
import { User } from "./models/User"; |
|
||||
import { getUserByRowID, getUserByTelegramID } from "./models/User.helper"; |
|
||||
|
import { User, UserInstance } from "./models/User"; |
||||
|
import { getUserByTelegramID } from "./models/User.helper"; |
||||
|
|
||||
export function addContact( |
|
||||
|
export async function addContact( |
||||
userATelegram: TelegramID, |
userATelegram: TelegramID, |
||||
userBTelegram: TelegramID, |
|
||||
callback: (success: boolean, message?: string) => void |
|
||||
): void { |
|
||||
getUserByTelegramID(userATelegram, (userA) => { |
|
||||
getUserByTelegramID(userBTelegram, (userB) => { |
|
||||
|
userBTelegram: TelegramID |
||||
|
): Promise<void> { |
||||
|
const userA = await getUserByTelegramID(userATelegram); |
||||
|
const userB = await getUserByTelegramID(userBTelegram); |
||||
|
|
||||
if (!userA || !userB) { |
if (!userA || !userB) { |
||||
callback(false, "Could not find user."); |
|
||||
return; |
|
||||
|
throw new Error("Could not found users"); |
||||
} |
} |
||||
|
|
||||
Contact.create({ user: userA.id, with: userB.id }) |
|
||||
.then(() => { |
|
||||
console.log( |
|
||||
`Registering contact between ${userA.id} and ${userB.id}` |
|
||||
); |
|
||||
sendTelegramMessage(userB.telegram, strings_en.telegram_qr_scanned); |
|
||||
callback(true, "Successfully added contact"); |
|
||||
}) |
|
||||
.catch((e) => { |
|
||||
callback(false, e); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
||||
|
await Contact.create({ user: userA.id, with: userB.id }); |
||||
|
await sendTelegramMessage(userB.telegram, strings_en.telegram_qr_scanned); |
||||
} |
} |
||||
|
|
||||
export function createUser( |
|
||||
telegram: TelegramID, |
|
||||
callback: (success: boolean, message: string) => void |
|
||||
): void { |
|
||||
User.create({ |
|
||||
|
export async function createUser( |
||||
|
telegram: TelegramID |
||||
|
): Promise<UserInstance | null> { |
||||
|
const user = await User.create({ |
||||
telegram: telegram, |
telegram: telegram, |
||||
}) |
|
||||
.then((user) => { |
|
||||
if (!user) { |
|
||||
callback(false, "Could not create user"); |
|
||||
} else { |
|
||||
callback(true, "Success"); |
|
||||
} |
|
||||
}) |
|
||||
.catch((reason) => { |
|
||||
if (reason.name == "SequelizeUniqueConstraintError") { |
|
||||
callback(false, "User already exists"); |
|
||||
} else { |
|
||||
callback(false, "Unknown error"); |
|
||||
} |
|
||||
}); |
}); |
||||
|
return user; |
||||
} |
} |
||||
|
@ -1,42 +1,30 @@ |
|||||
import axios from "axios"; |
import axios from "axios"; |
||||
import { TelegramID } from "./types"; |
import { TelegramID } from "./types"; |
||||
|
|
||||
export function setTelegramWebHook( |
|
||||
callback: (success: boolean) => void = () => {} |
|
||||
): void { |
|
||||
|
export async function setTelegramWebHook(): Promise<void> { |
||||
const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/setWebhook`; |
const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/setWebhook`; |
||||
axios |
|
||||
.post(url, { |
|
||||
|
await axios.post(url, { |
||||
url: `${process.env.SERVER_API_URL}/${process.env.TELEGRAM_SECRET}`, |
url: `${process.env.SERVER_API_URL}/${process.env.TELEGRAM_SECRET}`, |
||||
allowed_updates: [], |
allowed_updates: [], |
||||
drop_pending_updates: true, |
drop_pending_updates: true, |
||||
}) |
|
||||
.then((res) => { |
|
||||
callback(!!res); |
|
||||
}) |
|
||||
.catch((err) => { |
|
||||
callback(!!err); |
|
||||
}); |
}); |
||||
} |
} |
||||
|
|
||||
export function sendTelegramMessage( |
|
||||
|
export async function sendTelegramMessage( |
||||
telegramID: TelegramID, |
telegramID: TelegramID, |
||||
message: string, |
|
||||
callback: (success: boolean) => void = () => {} |
|
||||
): void { |
|
||||
|
message: string |
||||
|
): Promise<void> { |
||||
const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`; |
const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`; |
||||
axios |
|
||||
.post(url, { |
|
||||
|
const response = await axios.post(url, { |
||||
chat_id: telegramID, |
chat_id: telegramID, |
||||
text: message, |
text: message, |
||||
}) |
|
||||
.then((res) => { |
|
||||
callback(!!res); |
|
||||
}) |
|
||||
.catch((err) => { |
|
||||
console.error("Problem sending Telegram message."); |
|
||||
callback(!!err); |
|
||||
}); |
}); |
||||
} |
} |
||||
|
|
||||
setTelegramWebHook(); |
|
||||
|
|
||||
|
setTelegramWebHook() |
||||
|
.catch(error=>{ |
||||
|
console.error("Error setting Telegram Webhook"); |
||||
|
error instanceof Error && console.error(error.message); |
||||
|
}); |
||||
|
|
||||
|
Loading…
Reference in new issue